标签:ESS follow tail and detail HERE 思路 empty bar
Input
Output
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
Hint
1 #include<iostream> 2 #include<stdio.h> 3 #include<queue> 4 #include<string.h> 5 using namespace std ; 6 7 const int INF = 0x3f3f3f3f; 8 int G[2000][2000]; 9 int d[2000]; 10 11 int i ,j; 12 13 struct node{ 14 int num; 15 int dis; 16 friend bool operator<(node a ,node b) 17 { 18 return a.dis>b.dis; 19 } 20 }; 21 22 int main() 23 { 24 int M , N; 25 int x,y,D; 26 27 priority_queue<node>que; 28 29 30 while(scanf("%d%d",&M,&N)!=EOF) 31 { 32 for( i = 1 ;i <= N ;i++) 33 { 34 for( j = 1 ;j <= N ;j++) 35 { 36 G[i][j] = INF; 37 } 38 } 39 40 for(int i = 1 ; i <= N ;i++) 41 { 42 G[i][i] = 0; 43 } 44 for( i = 1; i <= M ;i++) 45 { 46 scanf("%d%d%d",&x,&y,&D); 47 48 if(G[x][y]>D) 49 { 50 G[x][y] = D; 51 G[y][x] = D; 52 } 53 54 } 55 memset(d,0x3f,sizeof(d)); 56 d[1] = 0; 57 que.push({1,0}); 58 while(!que.empty()) 59 { 60 node tp = que.top(); 61 62 que.pop(); 63 64 for(i = 1 ;i <= N ;i++) 65 { 66 if(G[tp.num][i]) 67 { 68 if(d[i]>d[tp.num]+G[tp.num][i]) 69 { 70 d[i] = d[tp.num] + G[tp.num][i]; 71 que.push({i,d[i]}); 72 } 73 } 74 } 75 } 76 77 printf("%d\n",d[N]); 78 while(!que.empty()) 79 { 80 que.pop(); 81 } 82 83 84 } 85 return 0; 86 }
POJ - 2387 Til the Cows Come Home (最短路Dijkstra+优先队列)
标签:ESS follow tail and detail HERE 思路 empty bar
原文地址:https://www.cnblogs.com/yewanting/p/10798524.html