POJ-2387 Til the Cows Come Home(Dijsktra算法求最短路径)

版权声明:Copyright : 李三金,原创文章转载标明出处即可。 https://blog.csdn.net/santa9527/article/details/54234328

Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 47506 Accepted: 16161

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.

Source
USACO 2004 November

基础最短路题,因为没有负权值,而且数据范围小,所以能用Djisktra算法过。

什么是Dijkstra?

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

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

算法步骤?
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的距离;若从源点v到顶点u的距离(经过顶点k)比原来距离(不经过顶点k)短,则修改顶点u到源点v的距离值,修改后的距离值的为源点v到顶点k的距离加上k到顶点u的距离。

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

附上大神博客里搬来的模拟图(图侵删):
这里写图片描述

建议手动模拟一遍,很好懂得算法。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#define N 2048
//#define INF INT_MAX//不知道为什么,定义成int的最大值会输出错误答案
#define INF 9999999//定义无穷
using namespace std;
int i,j,k;
int x,y,z,t,n;
bool v[N];//访问标记
int w[N][N],d[N];//领接矩阵,源点到其他点的距离
void Dijkstra()//核心算法
{
    memset(v,0,sizeof(v));//标记数组清空

    for(i=1; i<=n; i++) d[i]=(i==n ? 0:INF);//将源点到其他点距离初始值赋值为INF,步骤a

    for(i=1; i<=n; i++)//外层循环,将所有点遍历,不断更新
    {
        int xx,minn=INF;
        //找出当前距离源点最小的点,步骤,步骤b
        for(j=1; j<=n; j++)
            if(!v[j]&&d[j]<=minn) minn=d[xx=j];//暂时不知道为什么要加等号
        v[xx]=1;
        //步骤c
        for(j=1; j<=n; j++)
            d[j]=min(d[j],d[xx]+w[xx][j]);
    }
}
int main()
{
    while(~scanf("%d %d",&t,&n))
    {
        //数据处理,不连通的两顶距离为INF
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                w[i][j]=(i==j ? 0:INF);

        for(i=0; i<t; i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            if(w[x][y]>z)//可以先输入 1 2 100 再输入 1 2 10 所以只保留最短的
                w[y][x]=w[x][y]=z;
        }
        Dijkstra();
//        for(i=1;i<=n;i++)
        printf("%d\n",d[1]);//Dijkstra算法可以找出源点到其余各点的最短路
    }
    return 0;
}

以上内容由看大神博客学习后写下。
附上大神博客地址:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html

猜你喜欢

转载自blog.csdn.net/santa9527/article/details/54234328
今日推荐