Til the Cows Come Home(最短路)

Til the Cows Come Home
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 2387

Descriptionios

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.

Outputapp

* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
 1 #include <iostream>
 2 #include <string>
 3 #include <queue>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <algorithm>
 8 #include <deque>
 9 #include <vector>
10 #define LL long long
11 #define MAXI 2147483647
12 #define MAXL 9223372036854775807
13 #define dg(i) cout << "*" << i << endl;
14 using namespace std;
15 
16 /**
17 *spfa算法:鄰接矩陣+SLF策略
18 *頂點標號:1..n
19 *源點:1,目的點:n
20 */
21 
22 const int sz = 1005;  //size--最大頂點數
23 const int inf = 0x3f3f3f3f;  //最大值
24 int w[sz][sz];  //w[i][j]--邊(i,j)的權值,若(i,j)不存在,w[i][j]爲inf
25 int dis[sz];  //dis[i]--源點到頂點i的最短距離,初始化爲inf
26 bool in[sz];  //in[i]--標記頂點i是否在隊列中,在爲true
27 int n, m;  //n--頂點數,m--邊數
28 
29 int main()
30 {
31     while(scanf("%d %d", &m, &n) != EOF)
32     {
33         int u, v, d;
34         memset(dis, inf, sizeof(dis));  //0x3f3f3f3f是能夠用memset初始化的
35         dis[1] = 0;  //
36         memset(w, inf, sizeof(w));
37         while(m--)
38         {
39             scanf("%d %d %d", &u, &v, &d);
40             w[u][v] = w[v][u] = min(d, w[u][v]);  //爲避免平行邊,取min
41         }
42         memset(in, false, sizeof(in));
43         deque<int> que;
44         que.push_front(1);  //源點進隊
45         in[1] = true;
46         while(!que.empty())
47         {
48             int cur = que.front();
49             que.pop_front();
50             in[cur] = false;  //撤銷進隊標誌
51             for(int i = 1; i <= n; i++)  //對與cur相鄰的點都進行鬆弛計算
52             {
53                 if(dis[cur] + w[cur][i] < dis[i])
54                 {
55                     dis[i] = dis[cur] + w[cur][i];  //更新最短距離
56                     if(!in[i])  //對於最短距離被更新的點,若不在隊列則進隊
57                     {
58                         //此處執行SLF策略,小的儘可能放前面。不採起這個策略而直接讓i進隊也能夠
59                         if(!que.empty() && dis[i] < dis[que.front()])
60                             que.push_front(i);
61                         else que.push_back(i);
62                         in[i] = true;
63                     }
64                 }
65             }
66         }
67         printf("%d\n", dis[n]);
68     }
69     return 0;
70 }
相關文章
相關標籤/搜索