hdu-2544 最短路---模板题

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2544

题目大意:

1-N的最短路长度

解题思路:

dijkstra模板

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 100 + 10;
 5 const int INF = 0x3f3f3f3f;
 6 int Map[maxn][maxn];
 7 int v[maxn], d[maxn];
 8 int n, m;
 9 void dijkstra(int u)
10 {
11     memset(v, 0, sizeof(v));
12     for(int i = 0; i <= n; i++)d[i] = (i == u ? 0 : INF);
13     for(int i = 0; i < n; i++)
14     {
15         int x, m = INF;
16         for(int j = 1; j <= n; j++)if(!v[j] && d[j] <= m)m = d[x = j];
17         v[x] = 1;
18         for(int j = 1; j <= n; j++)
19         {
20             if(d[j] > d[x] + Map[x][j])
21             {
22                 d[j] = d[x] + Map[x][j];
23             }
24         }
25     }
26     cout<<d[n]<<endl;
27 }
28 int main()
29 {
30     while(cin >> n >> m && n)
31     {
32         int u, v, w;
33         memset(Map, INF, sizeof(Map));
34         while(m--)
35         {
36             cin >> u >> v >> w;
37             Map[u][v] = Map[v][u] = w;
38         }
39         dijkstra(1);
40     }
41 }

 

posted @ 2018-04-22 15:53  _努力努力再努力x  阅读(119)  评论(0编辑  收藏  举报