POJ 2387 Til the Cows Come Home (dijkstra模板题)

Posted 抓不住Jerry的Tom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2387 Til the Cows Come Home (dijkstra模板题)相关的知识,希望对你有一定的参考价值。

Description

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

理解dijkstra,明天写完
代码如下:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <algorithm>
 6 #include <vector>
 7 using namespace std;
 8 #define inf 0x3f3f3f3f
 9 const int maxn =1010;
10 vector <int>G[maxn];
11 bool done[maxn];
12 int p[maxn];
13 int d[maxn];
14 int t,n;
15 struct Edge{
16     int from,to,dis;
17     Edge(int u,int v,int d):from(u),to(v),dis(d){}
18 };
19 vector <Edge>edges;
20 struct HeapNode {
21     int d,u;
22     bool operator <(const HeapNode& x) const {
23         return d>x.d;
24     }
25 };
26 void addEdge(int u,int v,int dis){
27     edges.push_back(Edge(u,v,dis));
28     int m=edges.size();
29     G[u].push_back(m-1);
30 }
31 void dijkstra (int s) {
32     priority_queue<HeapNode> q;
33     memset(d,inf,sizeof d);
34     d[s]=0;
35     memset(done,false,sizeof done);
36     q.push(HeapNode{0,s});
37     while (!q.empty()){
38         HeapNode x=q.top();
39         q.pop();
40         int u=x.u;
41         if (done[u])
42             continue;
43         done[u]=true;
44         for (int i=0;i<G[u].size();++i){
45             Edge& e=edges[G[u][i]];
46             if (d[e.to]>d[u]+e.dis){
47                 d[e.to]=d[u]+e.dis;
48                 p[e.to]=G[u][i];
49                 q.push((HeapNode){d[e.to],e.to});
50             }
51         }
52     }
53 }
54 void init(){
55     for (int i=0;i<n;++i)
56     G[i].clear();
57     edges.clear();
58 }
59 int main()
60 {
61     //freopen("de.txt","r",stdin);
62     while (~scanf("%d%d",&t,&n)){
63         init();
64         for (int i=0;i<t;++i){
65             int x,y,z;
66             scanf("%d%d%d",&x,&y,&z);
67             addEdge(x,y,z);
68             addEdge(y,x,z);
69         }
70         dijkstra(1);
71         printf("%d\n",d[n]);
72     }
73     return 0;
74 }

 

 

以上是关于POJ 2387 Til the Cows Come Home (dijkstra模板题)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2387 Til the Cows Come Home

POJ - 2387 Til the Cows Come Home

POJ-2387 Til the Cows Come Home ( 最短路 )

[POJ - 2387] L - Til the Cows Come Home(图论)

POJ 2387 Til the Cows Come Home

POJ-2387-Til the Cows Come Home(最短路)