poj 1502 MPI Maelstrom 最短路
题意:求其试点发出的到其他各点的最短路的最大值。
分析:最短路
View Code
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define re(i,n) for(int i=0;i<n;i++) #define re1(i,n) for(int i=1;i<=n;i++) #define inf (1<<29) const int maxn = 110; int map[maxn][maxn] , dist[maxn] , sta[maxn]; char c[maxn]; bool mark[maxn]; int n; void spfa() { int top = 0; re(i,n) dist[i] = inf; memset(mark,false,sizeof(mark)); dist[0] = 0; mark[0] = true; sta[++top] = 0; while(top > 0) { int u = sta[top --]; mark[u] = false; re(i,n) if(dist[u] + map[u][i] < dist[i]) { dist[i] = dist[u] + map[u][i]; if(!mark[i]) { mark[i] = true; sta[++top] = i; } } } } void input() { re1(i,n-1) re(j,i) { scanf("%s",c); if(c[0] == 'x') map[i][j] = map[j][i] = inf; else map[i][j] = map[j][i] = atoi(c); } } void output() { int ans = 0; re1(i,n-1) if(dist[i] > ans) ans = dist[i]; printf("%d\n",ans); } int main() { while(~scanf("%d",&n)) { input(); spfa(); output(); } return 0; }

浙公网安备 33010602011771号