标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42384 Accepted Submission(s): 15689
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <queue> 6 using namespace std; 7 const int maxn = 205; 8 const int INF = 0x3f3f3f3f; 9 struct edge{ 10 int to,cost; 11 friend bool operator < (edge A,edge B){ 12 return A.cost > B.cost; 13 } 14 }; 15 int d[maxn]; 16 vector<edge> g[maxn]; 17 bool done[maxn]; 18 void dijkstra(int s){ 19 memset(d,INF,sizeof(d)); 20 memset(done,false,sizeof(done)); 21 d[s] = 0; 22 priority_queue<edge> q; 23 q.push((edge){s,0}); 24 while(!q.empty()){ 25 edge cur = q.top(); q.pop(); 26 int v = cur.to; 27 if(done[v]) continue; 28 done[v] = true; 29 for(int i = 0; i<g[v].size(); i++){ 30 edge e = g[v][i]; 31 if(d[e.to]>d[v]+e.cost){ 32 d[e.to] = d[v]+e.cost; 33 q.push((edge){e.to,d[e.to]}); 34 } 35 } 36 } 37 } 38 void solve(){ 39 int n,m; 40 while(scanf("%d%d",&n,&m)!=EOF){ 41 for(int i = 0; i<m; i++){ 42 int a,b,c; 43 scanf("%d%d%d",&a,&b,&c); 44 g[a].push_back((edge){b,c}); 45 g[b].push_back((edge){a,c}); 46 } 47 int s,t; 48 scanf("%d%d",&s,&t); 49 dijkstra(s); 50 if(d[t] == INF) printf("-1\n"); 51 else printf("%d\n",d[t]); 52 for(int i = 0; i<maxn; i++) g[i].clear(); 53 } 54 } 55 int main() 56 { 57 solve(); 58 return 0; 59 }
标签:
原文地址:http://www.cnblogs.com/littlepear/p/5460318.html