Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32824   Accepted: 11098

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.
 

Dijkstra算法

1.定义概览

Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹学等等。注意该算法要求图中不存在负权边。

问题描述:在无向图 G=(V,E) 中,假设每条边 E[i] 的长度为 w[i],找到由顶点 V0 到其余各点的最短路径。(单源最短路径)

 

2.算法描述

1)算法思想:设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中。在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v到U中任何顶点的最短路径长度。此外,每个顶点对应一个距离,S中的顶点的距离就是从v到此顶点的最短路径长度,U中的顶点的距离,是从v到此顶点只包括S中的顶点为中间顶点的当前最短路径长度。

2)算法步骤:

a.初始时,S只包含源点,即S={v},v的距离为0。U包含除v外的其他顶点,即:U={其余顶点},若v与U中顶点u有边,则<u,v>正常有权值,若u不是v的出边邻接点,则<u,v>权值为∞。

b.从U中选取一个距离v最小的顶点k,把k,加入S中(该选定的距离就是v到k的最短路径长度)。

c.以k为新考虑的中间点,修改U中各顶点的距离;若从源点v到顶点u的距离(经过顶点k)比原来距离(不经过顶点k)短,则修改顶点u的距离值,修改后的距离值的顶点k的距离加上边上的权。

d.重复步骤b和c直到所有顶点都包含在S中。

 

执行动画过程如下图

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int INF=99999999;                    //设为无穷大
 7 int maps[1005][1005],v[1005],d[1005];  //v表示是否已经过遍历 d表示从源到点当前最短路
 8 int n;
 9 
10 void Dijkstra(int s,int t)
11 {
12     int i,j,k,mini;
13     for(i=1;i<=n;i++)
14         d[i]=INF;                      //除源点设为0距离外 其他先设为无穷大
15     d[s]=0;
16     for(i=1;i<=n;i++)                  //n点循环n次
17     {                                                 
18         mini=INF;
19         k=-1;
20         for(j=1;j<=n;j++)              //在所有未标记点中 选d值最小的点
21         {
22             if(!v[j] && d[j]<mini)
23             {
24                 mini=d[k=j];
25             }
26         }
27         v[k]=1;                         //标记节点
28         if(k==t)
29         {
30             printf("%d\n",d[t]);
31             return;
32         }
33         for(j=1;j<=n;j++)
34         {
35             if(!v[j] && d[k]+maps[k][j]<d[j])   //表示从k出发的点,对于所有边,更新相连点
36             {
37                 d[j]=d[k]+maps[k][j];
38             }
39         }
40     }
41 }
42 
43 int main()
44 {
45     int T,i,j,x,y,D;
46     while(scanf("%d %d",&T,&n)!=EOF)
47     {
48         memset(v,0,sizeof(v));            //清除标记
49         for(i=1;i<=n;i++)
50         {
51             for(j=1;j<=n;j++)
52             {
53                 maps[i][j]=INF;
54             }
55         }
56         for(i=1;i<=T;i++)
57         {
58             scanf("%d %d %d",&x,&y,&D);
59             if(maps[x][y]>D)               //可能有多条路,只记录最短的
60                 maps[x][y]=D,maps[y][x]=D;
61         }
62         Dijkstra(1,n);
63     }
64     return 0;
65 }                                          //END Thankyou
View Code

 spfa

是一种求单源最短路的算法

算法中需要用到的主要变量

int n;  //表示n个点,从1到n标号

int s,t;  //s为源点,t为终点

int d[N];  //d[i]表示源点s到点i的最短路

int p[N];  //记录路径(或者说记录前驱)

queue <int> q;  //一个队列,用STL实现,当然可有手打队列,无所谓

bool vis[N];   //vis[i]=1表示点i在队列中 vis[i]=0表示不在队列中

 

几乎所有的最短路算法其步骤都可以分为两步

1.初始化

2.松弛操作

 

初始化: d数组全部赋值为INF(无穷大);p数组全部赋值为s(即源点),或者赋值为-1,表示还没有知道前驱

             然后d[s]=0;  表示源点不用求最短路径,或者说最短路就是0。将源点入队;

    (另外记住在整个算法中有顶点入队了要记得标记vis数组,有顶点出队了记得消除那个标记)

队列+松弛操作

读取队头顶点u,并将队头顶点u出队(记得消除标记);将与点u相连的所有点v进行松弛操作,如果能更新估计值(即令d[v]变小),那么就更新,另外,如果点v没有在队列中,那么要将点v入队(记得标记),如果已经在队列中了,那么就不用入队

以此循环,直到队空为止就完成了单源最短路的求解

 

SPFA可以处理负权边

定理: 只要最短路径存在,上述SPFA算法必定能求出最小值。

证明:

  每次将点放入队尾,都是经过松弛操作达到的。换言之,每次的优化将会有某个点v的最短路径估计值d[v]变小。所以算法的执行会使d越来越小。由于我们假定图中不存在负权回路,所以每个结点都有最短路径值。因此,算法不会无限执行下去,随着d值的逐渐变小,直到到达最短路径值时,算法结束,这时的最短路径估计值就是对应结点的最短路径值。(证毕)

期望的时间复杂度O(ke), 其中k为所有顶点进队的平均次数,可以证明k一般小于等于2。

 

判断有无负环:

  如果某个点进入队列的次数超过N次则存在负环(SPFA无法处理带负环的图)

 1 int spfa_bfs(int s)
 2 {
 3     queue <int> q;
 4     memset(d,0x3f,sizeof(d));
 5     d[s]=0;
 6     memset(c,0,sizeof(c));
 7     memset(vis,0,sizeof(vis));
 8 
 9     q.push(s);  vis[s]=1; c[s]=1;
10     //顶点入队vis要做标记,另外要统计顶点的入队次数
11     int OK=1;
12     while(!q.empty())
13     {
14         int x;
15         x=q.front(); q.pop();  vis[x]=0;
16         //队头元素出队,并且消除标记
17         for(int k=f[x]; k!=0; k=nnext[k]) //遍历顶点x的邻接表
18         {
19             int y=v[k];
20             if( d[x]+w[k] < d[y])
21             {
22                 d[y]=d[x]+w[k];  //松弛
23                 if(!vis[y])  //顶点y不在队内
24                 {
25                     vis[y]=1;    //标记
26                     c[y]++;      //统计次数
27                     q.push(y);   //入队
28                     if(c[y]>NN)  //超过入队次数上限,说明有负环
29                         return OK=0;
30                 }
31             }
32         }
33     }
34 
35     return OK;
36 
37 }
View Code

 

 

posted @ 2015-05-01 15:19  cyd2014  阅读(1357)  评论(0编辑  收藏  举报