#include <stdio.h> #include <conio.h> #include <string.h> #define N 1010 int g[N][N];
void dijkstra(int n) { int i,j; int min, minid, minest, dis[N], mark[N], path[N];
for(i=1; i<=n; i++) { dis[i] = g[1][i]; mark[i] = 0; if(dis[i] > 0) path[i] = 1; else path[i] = 0; } mark[1] = 1; path[1] = 0; for(i=1; i<n; i++) { min = 0; minid = 0; for(j=1; j<=n; j++) if(!mark[j] && dis[j] > min) { min = dis[j]; minid = j; } if(!minid || minid == n) break; mark[minid] = 1; for(j=1; j<=n; j++) if(!mark[j] && g[minid][j] > dis[j]) //这里这里,wa了好多 { dis[j] = g[minid][j]; path[j] = minid; } } if(!minid) printf("0\n"); else { minest = 0xfffffff; while(path[minid]) //找出最小权 { if(g[path[minid]][minid] < minest ) minest = g[path[minid]][minid]; minid = path[minid]; } printf("%d\n\n", minest); } }
int main() { int t, n, m, x, y, k=0, w;
freopen("in.txt", "r", stdin); scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); memset(g, 0, sizeof(g)); while(m--) { scanf("%d%d%d", &x, &y, &w); g[y][x] = g[x][y] = w; } k++; printf("Scenario #%d:\n", k); dijkstra(n); } getch(); return 0; }
|