POJ 3268 Silver Cow Party
题意:给定顶点个数,边的个数,目的地,求顶点到边的往返最大距离
解题思路:目的地到顶点的是标准的Dijkstra,顶点到目的地的只需要把矩阵转置一下就OKay了
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #define MAXVERTEXNUM 1005 5 #define INF 1000000 6 using namespace std; 7 8 int Nv, Ne, Des; 9 int MGraph[MAXVERTEXNUM][MAXVERTEXNUM]; 10 int dist1[MAXVERTEXNUM], dist2[MAXVERTEXNUM]; 11 12 void Dijkstra() 13 { 14 int collected[MAXVERTEXNUM]; 15 memset(collected, 0, sizeof(collected)); 16 collected[Des] = 1; 17 dist1[Des] = 0; 18 19 for (int i = 1; i <= Nv; ++i) 20 { 21 if (MGraph[Des][i] != INF) 22 dist1[i] = MGraph[Des][i]; 23 else 24 dist1[i] = INF; 25 } 26 27 for (int i = 2; i <= Nv; ++i) 28 { 29 int minDis = INF, num; 30 for (int j = 1; j <= Nv; ++j) 31 if (!collected[j] && minDis > dist1[j]) 32 { 33 minDis = dist1[j]; 34 num = j; 35 } 36 37 collected[num] = 1; 38 dist1[num] = minDis; 39 40 for (int j = 1; j <= Nv; ++j) 41 if (!collected[j] && dist1[j] > dist1[num] + MGraph[num][j]) 42 dist1[j] = dist1[num] + MGraph[num][j]; 43 } 44 } 45 46 void T() 47 { 48 for (int i = 1; i <= Nv; ++i) 49 for (int j = 1; j <= i; ++j) 50 swap(MGraph[i][j], MGraph[j][i]); 51 } 52 53 int main() 54 { 55 ios::sync_with_stdio(false); 56 57 cin >> Nv >> Ne >> Des; 58 59 for (int i = 1; i <= Nv; ++i) 60 for (int j = 1; j <= Nv; ++j) 61 if (i != j) 62 MGraph[i][j] = INF; 63 else 64 MGraph[i][j] = 0; 65 66 for (int i = 1; i <= Ne; ++i) 67 { 68 int V1, V2, Edge; 69 cin >> V1 >> V2 >> Edge; 70 MGraph[V1][V2] = Edge; 71 } 72 73 Dijkstra(); 74 for (int i = 1; i <= Nv; ++i) 75 dist2[i] = dist1[i]; 76 T(); 77 Dijkstra(); 78 79 int maxDis = 0; 80 for (int i = 1; i <= Nv; ++i) 81 if (i != Des) 82 { 83 int temp = 0; 84 temp += dist1[i]; 85 temp += dist2[i]; 86 if (temp > maxDis) 87 maxDis = temp; 88 } 89 90 cout << maxDis << endl; 91 92 return 0; 93 }
不忘初心,方得始终