Til the Cows Come Home(Dijkstra)

 Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. 

 Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it. 

 Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

Input

 Line 1: Two integers: T and N 

 Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.

Output

 Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

Sample Input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

Sample Output

90

Hint

INPUT DETAILS: 

 There are five landmarks. 

OUTPUT DETAILS: 

 Bessie can get home by following trails 4, 3, 2, and 1.   题意: 给t条路径以及权值,问从1到n最短路径   思路: dijkstra, 记录每个点到起点1的起始长度 找距离起点最近的一点,(该点距起点的距离已是最短,因此无需优化该点) 凭借这一点,优化别的路, 这次优化完成后,把这一用完的点标记,以后不再用了 再找当前最近的一点, 循环下去,直到优化全部完成 因此,dijkstra算法适合于单源求最短路  
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define INF 0x3f3f3f
 4 
 5 int m[1005][1005], dis[1005], vis[1005];
 6 
 7 void dijkstra(int n)
 8 {
 9     int i, minn, mini;
10     memset(vis, 0, sizeof(vis));
11     memset(dis, INF, sizeof(dis));
12     for(i=1; i<=n; i++)
13     {
14         dis[i] = m[i][1];
15     }
16     dis[1] = 0;
17     vis[1] = 1;
18     while(1)
19     {
20         minn = INF;
21         for(i=1; i<=n; i++)   //找出当前无法再优化的点,用该点辅助优化其他点
22         {
23             if(dis[i] < minn && vis[i]==0)
24             {
25                 minn = dis[i];
26                 mini = i;
27             }
28         }
29         if(minn == INF) break;  //所有点都利用过,已经没有能借助的点了
30         vis[mini] = 1;    //利用的标记一下
31         for(i=1;i<=n;i++)
32         {
33             if(dis[i]>dis[mini]+m[i][mini])  //借助mini点优化
34             {
35                 dis[i] = dis[mini]+m[i][mini];
36                 m[i][1] = m[1][i] = dis[i];
37             }
38         }
39     }
40 }
41 
42 int main()
43 {
44     int t, n;
45     int u, v, w;
46     scanf("%d %d", &t, &n);
47     memset(m, INF, sizeof(m));    //地图上初始化没有路,即所有权值无穷大
48     while(t--)
49     {
50         scanf("%d %d %d", &u, &v, &w);
51         if(w < m[u][v])
52         {
53             m[u][v] = w;
54             m[v][u] = w;
55         }
56     }
57     dijkstra(n);
58     printf("%d\n", dis[n]);
59     return 0;
60 }

 

上一篇:vue升级导致TypeError:Cannot read property ‘parseComponent’ of undefined


下一篇:使用VUE CLI3.0搭建项目vue2+scss+element简易版