Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 50686 Accepted Submission(s): 22280
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 using namespace std; 8 const int INF = 0x3f3f3f3f; 9 struct edge{ 10 int to,cost; 11 }; 12 typedef pair<int,int> p; 13 int V; 14 vector<edge> G[105]; 15 int d[105]; 16 void dijkstra(int s){ 17 priority_queue<p,vector<p>,greater<p> > q; 18 memset(d,INF,sizeof(d)); 19 d[s] = 0; 20 q.push(p(0,s)); 21 while(!q.empty()){ 22 p cur = q.top(); q.pop(); 23 int v = cur.second; 24 if(d[v]<cur.first) continue; 25 for(int i = 0; i<G[v].size(); i++){ 26 edge e = G[v][i]; 27 if(d[e.to]>d[v]+e.cost){ 28 d[e.to] = d[v] + e.cost; 29 q.push(p(d[e.to],e.to)); 30 } 31 } 32 } 33 } 34 void solve(){ 35 int n,m; 36 while(scanf("%d%d",&n,&m) == 2&&n&&m){ 37 for(int i = 0; i<105; i++) G[i].clear(); 38 for(int i = 0; i<m; i++) 39 { 40 int A,B,C; 41 scanf("%d%d%d",&A,&B,&C); 42 G[A].push_back((edge){B,C}); 43 G[B].push_back((edge){A,C}); 44 } 45 dijkstra(1); 46 printf("%d\n",d[n]); 47 } 48 } 49 int main() 50 { 51 solve(); 52 return 0; 53 }
原文:http://www.cnblogs.com/littlepear/p/5457012.html