裸裸的spfa～嘿嘿嘿!

spfa全名：shortest path faster algorithm ,又是一个国内神犇写的算法。

／＊松弛操作：是一种对当前节点以前存的值和其它路径走来的值的比较。取优值。

＊／

a先进队，之后开始对a接下来的点进行操作。

a有 b c d

b上的值变成24

！！（重点）让能松弛的点都入队。

a的点的操作就完成了。

b  的边就是  e

e点上的值就变成了24+6=30；表示现阶段。从a到e最优值就是30

e在visit打上标记。

b出队。

b的操作就结束了。

e上的值就变成了／15

f的值就变成11.f入队。visit上f打标记。

c出队，

c的操作结束。

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

Hint

INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.

裸题。

 1 #include<cstdio>
2 #include<algorithm>
3 #include<string.h>
4 #include<queue>
5 using namespace std;
6 struct node {
7     int next;
8     int v;
9     int val;
10 }edge[100001];
12 queue<int> q;
13 void add(int x,int y,int z)
14 {
15     edge[++cnt].v=y;
17     edge[cnt].val=z;
19 }
20 int main()
21 {
22     int x,y,z;
23     while(scanf("%d%d",&n,&m)==2)
24     {
26     memset(d,214546,sizeof(d));
27     memset(visit,0,sizeof(visit));
28     while(!q.empty() ){
29         q.pop() ;
30     }
31     for(int i=1;i<=n;i++)
32     {
33         scanf("%d%d%d",&x,&y,&z);
36     }
37     q.push(1);
38     visit[1]=1;
39     d[1]=0;
40     while(!q.empty())
41     {
42         int a=q.front();
43         q.pop();
44         visit[a]=0;
46         {
47             if(d[edge[i].v]>d[a]+edge[i].val)
48               {
49                   if(!visit[edge[i].v]){
50                       d[edge[i].v]=d[a]+edge[i].val;
51                       q.push(edge[i].v);
52                       visit[edge[i].v]=1;
53                   }
54                   else {
55                       d[edge[i].v]=d[a]+edge[i].val;
56                   }
57               }
58         }
59     }
60     printf("%d ",d[m]);
61      }
62     return 0;
63 }

posted @ 2016-09-12 13:08 刺猬的玻璃心碎了 阅读(...) 评论(...) 编辑 收藏