G - MPI Maelstrom
1 #include<algorithm> //SPFA 2 #include<queue> 3 #include<stdio.h> 4 #include<string.h> 5 #include<vector> 6 using namespace std; 7 8 const int maxn = 105; 9 const int INF = 0xfffffff; 10 11 struct node 12 { 13 int y, time; 14 node(int y, int t):y(y), time(t){} 15 }; 16 vector<node> G[maxn]; //G[i][j] 从i到G[i][j].y 的时间为G[i][j].time 17 int v[maxn]; 18 19 void spfa(int s) 20 { 21 queue<int> Q; 22 Q.push(s); 23 24 while(Q.size()) 25 { 26 s = Q.front();Q.pop(); 27 int len = G[s].size(); 28 29 for(int i=0; i<len; i++) 30 { 31 node q = G[s][i]; 32 if(v[q.y] > v[s]+q.time) 33 { 34 v[q.y] = v[s] + q.time; 35 Q.push(q.y); 36 } 37 } 38 } 39 } 40 41 int main() 42 { 43 int N; 44 45 while(scanf("%d", &N) != EOF) 46 { 47 int i, j, x; 48 char s[30]; 49 50 for(i=1; i<=N; i++) 51 { 52 v[i] = INF; 53 G[i].clear(); 54 } 55 v[1] = 0; 56 57 for(i=2; i<=N; i++) 58 for(j=1; j<i; j++) 59 { 60 scanf("%s", s); 61 if(s[0] != 'x') 62 { 63 sscanf(s, "%d", &x); 64 G[i].push_back(node(j, x)); 65 G[j].push_back(node(i, x)); 66 } 67 } 68 69 spfa(1); 70 71 int ans = -INF; 72 73 for(i=1; i<=N; i++) 74 ans = max(ans, v[i]); 75 76 printf("%d\n", ans); 77 } 78 79 return 0; 80 }