题意有点晕啊。。 水题一道。。
分身去教室的时候是单向的,回来的时候也是单向的,这时候就要反过来跑一遍最短路了。。
Dijkstra看着模板写的。。sigh~ 要不是渣渣。。还是记不住。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits> 11 #include<string.h> 12 #include<cmath> 13 #include<stdlib.h> 14 #include<vector> 15 #define INF 1e7 16 #define MAXN 100010 17 #define maxn 1000010 18 #define Mod 1000007 19 #define N 1010 20 using namespace std; 21 typedef long long LL; 22 23 int n, m, x; 24 int G[N][N]; 25 int d[N], d1[N], d2[N]; 26 bool vis[N]; 27 28 bool Dijkstra(int x, int* d) 29 { 30 int i, j, pos, min; 31 for (i = 1; i <= n; ++i) 32 d[i] = G[x][i]; 33 d[x] = 0; 34 vis[x] = true; 35 for (i = 1; i <= n; ++i) { 36 min = INF; 37 for (j = 1; j <= n; ++j) { 38 if (d[j] < min && !vis[j]) { 39 pos = j; 40 min = d[j]; 41 } 42 } 43 if (min == INF) return false; 44 vis[pos] = true; 45 for (j = 1; j <= n; ++j) 46 if (d[pos] + G[pos][j] < d[j] && !vis[j]) 47 d[j] = d[pos] + G[pos][j]; 48 } 49 return true; 50 } 51 52 void process() 53 { 54 int u, v, t; 55 for (int i = 0; i <= n; ++i) 56 for (int j = 0; j <= n; ++j) 57 G[i][j] = INF; 58 for (int i = 0; i < m; ++i) { 59 cin >> u >> v >> t; 60 G[u][v] = t; 61 } 62 memset(vis, 0, sizeof(vis)); 63 Dijkstra(x, d1); 64 for (int i = 0; i <= n; ++i) 65 for (int j = i + 1; j <= n; ++j) 66 swap(G[i][j], G[j][i]); 67 memset(vis, 0, sizeof(vis)); 68 Dijkstra(x, d2); 69 for (int i = 0; i <= n; ++i) 70 d[i] = d1[i] + d2[i]; 71 /*for (int i = 1; i <= n; ++i) 72 cout << d[i] << " "; 73 cout << endl;*/ 74 int ans = 0; 75 for (int i = 1; i <= n; ++i) 76 ans = max(ans, d[i]); 77 cout << ans << endl; 78 } 79 80 int main() 81 { 82 while (cin >> n >> m >> x) 83 process(); 84 return 0; 85 }
原文:http://www.cnblogs.com/usedrosee/p/4291014.html