POJ 1502 MPI Maelstrom
题目大意:计算从1开始到其他点的最大距离,主要是输入比较扯拐,是邻接矩阵的下三角
解题思路:Dijkstra,思路是Dijkstra模板题,但是,输入比较扯拐
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #define MAXVERTEXNUM 110 5 #define INF 1000000 6 using namespace std; 7 8 int MGraph[MAXVERTEXNUM][MAXVERTEXNUM]; 9 int dist[MAXVERTEXNUM]; 10 int Nv; 11 12 void Dijkstra() 13 { 14 int res = 0; 15 int collected[MAXVERTEXNUM]; 16 memset(collected, 0, sizeof(collected)); 17 18 collected[1] = 1; 19 dist[1] = 0; 20 for (int i = 2; i <= Nv; ++i) 21 { 22 if (MGraph[i][1] != INF) 23 dist[i] = MGraph[i][1]; 24 else 25 dist[i] = INF; 26 } 27 28 for (int i = 2; i <= Nv; ++i) 29 { 30 int MinDis = INF, num; 31 for (int j = 2; j <= Nv; ++j) 32 if (!collected[j] && dist[j] < MinDis) 33 { 34 MinDis = dist[j]; 35 num = j; 36 } 37 if (res < dist[num]) 38 res = dist[num]; 39 collected[num] = 1; 40 41 for (int j = 2; j <= Nv; ++j) 42 { 43 if (!collected[j] && dist[j] > dist[num] + MGraph[num][j]) 44 dist[j] = dist[num] + MGraph[num][j]; 45 } 46 } 47 48 cout << res << endl; 49 } 50 51 int main() 52 { 53 ios::sync_with_stdio(false); 54 55 cin >> Nv; 56 57 //init 58 for (int i = 1; i <= Nv; ++i) 59 for (int j = 1; j <= Nv; ++j) 60 { 61 if (i == j) 62 MGraph[i][j] = 0; 63 else 64 MGraph[i][j] = INF; 65 } 66 67 for (int i = 2; i <= Nv; ++i) 68 for (int j = 1; j < i; ++j) 69 { 70 char Edge[1000]; 71 memset(Edge, 0, sizeof(Edge)); 72 cin >> Edge; 73 int num = 0; 74 if (Edge[0] != 'x') 75 { 76 for (int i = 0; Edge[i] != '\0'; ++i) 77 num = num * 10 + Edge[i] - '0'; 78 } 79 else 80 { 81 MGraph[i][j] = MGraph[j][i] = INF; 82 continue; 83 } 84 85 MGraph[i][j] = MGraph[j][i] = num; 86 } 87 88 Dijkstra(); 89 90 return 0; 91 }
不忘初心,方得始终