Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:Accept代码!理解了就超简单!In Reply To:Re:Accept代码!理解了就超简单! Posted by:2507409218 at 2018-05-21 17:55:07 干 兄弟 你竟然把代码最后的判断条件删了 AC代码如下 #include <cstdio> #include <iostream> #include <ctime> #include <cstring> #include<cstdlib> #pragma warning(disable:4996) using namespace std; #define MAX 0x7fffffff #define Min(a, b) ((a) < (b) ? (a) : (b)) int map[1010][1010], dis[1010]; int mark[1010]; int n, m, s, t, W = 0; void Dijkstra() { int i, j, k, min; for (i = 1; i <= n; i++) { mark[i] = 0; if (map[1][i] != 0) dis[i] = map[1][i]; else dis[i] = MAX; }//预处理 把1可以到达的节点全都设置成dis[i],到达不了的节点全都设置成MAX mark[1] = 1; for (i = 1; i < n; i++)//遍历除第一个节点之外的剩下的n-1个节点 { min = 0;//最大权值 for (j = 1; j <= n; j++) if (!mark[j] && dis[j] > min&&dis[j] != MAX)//从1节点引申出来的节点中的最大值 { min = dis[j]; k = j;//记录下是第几个节点权值最大 } mark[k] = 1; for (j = 1; j <= n; j++)//遍历其余的节点 if (!mark[j]) { int Q; Q = Min(dis[k], map[k][j]);//Q表示从起始节点到该节点的值和从该节点到目标节点的最小值 //Q表示走1-k-j这条路的最大限流 if ((dis[j]<Q||dis[j]==MAX)&&map[k][j] != 0)//如果最大限流大于1-j节点,且k-j有路,更新dis[j]的值 { dis[j] = Q; } } } printf("Scenario #%d:\n%d\n\n", W, dis[n]); } int main() { int i, j, x, y, d, N; scanf("%d", &N); while (N--) { scanf("%d %d", &n, &m); W++; memset(map, 0, sizeof(map)); for (i = 1; i <= m; i++) { scanf("%d %d %d", &x, &y, &d); map[x][y] = map[y][x] = d; } Dijkstra(); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator