MPI Maelstrom(Dijkstra)
http://poj.org/problem?id=1502
刷一道模板题稳定一下心情。。。
Dijkstra求单源最短路,就是输入的时候注意下,是按下三角输入的(无向图),输入字符x表示i与j不通。
可以这样输入:
if(scanf("%d",&w)) map[i][j] = map[j][i] = w;
else scanf("x");
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 const int INF = 0x3f3f3f3f; 4 5 int map[110][110],vis[110],dis[110],n; 6 void Dijkstra() 7 { 8 int i,j; 9 memset(vis,0,sizeof(vis)); 10 for(i = 0; i < n; i++) 11 dis[i] = INF; 12 dis[0] = 0; 13 14 for(i = 0; i < n; i++) 15 { 16 int mindis = INF,pos; 17 for(j = 0; j < n; j++) 18 { 19 if(!vis[j] && dis[j] < mindis) 20 { 21 mindis = dis[j]; 22 pos = j; 23 } 24 } 25 vis[pos] = 1; 26 for(j = 0; j < n; j++) 27 { 28 if(!vis[j] && dis[j] > dis[pos]+map[pos][j]) 29 dis[j] = dis[pos]+map[pos][j]; 30 } 31 } 32 33 } 34 35 int main() 36 { 37 while(~scanf("%d",&n)) 38 { 39 for(int i = 0; i < n; i++) 40 for(int j = 0; j < n; j++) 41 map[i][j] = INF; 42 for(int i = 1; i < n; i++) 43 { 44 for(int j = 0; j < i; j++) 45 { 46 int w; 47 if(scanf("%d",&w)) map[i][j] = map[j][i] = w; 48 else scanf("x"); 49 } 50 } 51 Dijkstra(); 52 int max = -1; 53 for(int i = 0; i < n; i++) 54 { 55 if(dis[i] > max) 56 max = dis[i]; 57 } 58 printf("%d\n",max); 59 } 60 return 0; 61 }