http://acm.hdu.edu.cn/showproblem.php?pid=2544
1 #include<iostream> 2 #include<queue> 3 #include<functional> 4 #include<algorithm> 5 #include<cstring> 6 #include<vector> 7 using namespace std; 8 const int N = 1e3+5; 9 const int INF = 1e7; 10 typedef pair<int, int> pll; 11 struct Node 12 { 13 int next; 14 int s; 15 Node(int nt, int d):next(nt), s(d){}; 16 }; 17 vector<Node> G[N]; 18 int dis[N]; 19 void dijkstra() 20 { 21 priority_queue<pll, vector<pll>, greater<pll> >q; 22 q.push(pll (0,1));// 距离 位置 23 while(!q.empty()) 24 { 25 pll temp = q.top(); q.pop(); 26 int x = temp.second, ss = temp.first; 27 if(dis[x]!= ss) 28 continue; 29 for(int i = 0; i < G[x].size(); i++) 30 { 31 int y = G[x][i].next, d = G[x][i].s; 32 if( dis[y] > dis[x] + d) 33 { 34 dis[y]= dis[x]+d; 35 q.push(pll (dis[y],y)); 36 } 37 } 38 } 39 } 40 void init(int n) 41 { 42 for(int i = 1; i <= n; i++) 43 { 44 dis[i] = INF; 45 G[i].clear(); 46 } 47 } 48 int main() 49 { 50 ios::sync_with_stdio(false); 51 cin.tie(0); 52 cout.tie(0); 53 int n, m; 54 while(cin >> n >> m, n+m) 55 { 56 init(n); 57 int x, y, d; 58 for(int i = 1; i <= m; i++) 59 { 60 cin >> x >> y >> d; 61 G[x].push_back(Node(y,d)); 62 G[y].push_back(Node(x,d)); 63 } 64 dis[1] = 0; 65 dijkstra(); 66 cout << dis[n] << endl; 67 } 68 return 0; 69 }