POJ2387 Til the Cows Come Home [kuangbin] 最短路

12 篇文章 0 订阅
1 篇文章 0 订阅

题目描述:
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.
* 输入:
* 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.
输出:
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
* 样例输入:
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
样例输出:90
提示:
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.

题目中文大意:有N个点,给你t条路径(双向路径),两点之间可能有多条路径,问从n走到1的最短路径是多少?
方法1
单源最短路dijkstra算法
AC代码

/*
AC
POJ2387 dijkstra
2017年7月20日15:20:37 
*/
#include<stdio.h>
#define inf 1e9;
const int maxn=1010;

int map[maxn][maxn];
int n,m;

void dijkstra(){
    int min,v;
    int d[maxn];// 存储1->i 的距离
    bool  vis[maxn]; //访问标记

    //访问标记全部置为0,第一次初始d[maxn]
    for(int i=1;i<=n;i++) {
        vis[i]=0;
        d[i]=map[1][i];
    }

    //每次循环都会找到1-v的 最短距离,循环n次,就可以
    //求出1到所有点的距离,把距离最小的点记录为v 
    for(int i=1;i<=n;i++){
        min=inf;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&d[j]<min){
                v=j;
                min=d[j];
            }
        }
        //选出一个点加入 U 这个点即为 1-v的最短距离 
        vis[v]=1;

        for(int j=1;j<=n;j++){
            if(!vis[j]&&d[j]>map[v][j]+d[v]){
                d[j]=map[v][j]+d[v];
            }
        }//更新所有的最短距离 
    }
    printf("%d\n",d[n]);
}


int main(){
    int a,b,c;
//  while(~scanf("%d%d",&n,&m)){
    scanf("%d%d",&m,&n);
        //初始化map[][]矩阵,对角线的值全部初始化为0
        //任意两点的距离置为 无穷大 
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==j)
                    map[i][i]=0;
                else map[i][j]=map[j][i]=inf;
            }
        }
        /*
        // 录入数据,要考虑两点之间有多条路
        //所以每次更新的时候,判定一下,保存两点之间最短
        //的那条路径就行 
        */
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);
            if(map[a][b]>c) map[a][b]=map[b][a]=c;
        }
        //调用 单源最短路径 
        dijkstra();
//  }

    return 0;
}

方法2
bellman-ford算法

/*
POJ2387
2017年7月29日10:33:11
AC bellman-ford 

*/ 
#include<stdio.h>
const int maxn=1e4+10;
const int inf=1<<29;
typedef struct{
    int a,b,w;
}Edge;

Edge edge[maxn<<1];
int n,m;

void bellman_ford(){
    int d[maxn];
    /*
    d[maxn]存的是1到i的最短距离  0=<i<maxn 
    */
    //将原点的值置为0,其他的值置为无穷大 
    for(int i=2;i<=n;i++) d[i]=inf;
    d[1]=0;
    //松弛计算 
    /*
    虽然看起来挺复杂,其实很好懂的,edge[][]存的是每一条边的信息,那么我们
    假设  edge[j].a  edge[j].b  edge[j].w 分别代表这条边的顶点a,b 权值w
    考虑顶点a,如果存在一条路径,使得这条路径到顶点b 的距离最短,
    那么这条路径的长度即为d[b]  也就是代码中 的 d[edge[j].b],再加上当前边
    的权值w,那么就可以求出 d[a] 的最短距离,也就是代码中的 d[edge[j].a]
    如果存在   d[edge[j].a]>edge[j].w+d[edge[j].b]  那么我们对d[edge[j].a]的
    距离进行更新 d[edge[j].a]=edge[j].w+d[edge[j].b];  
    */
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(d[edge[j].a]>edge[j].w+d[edge[j].b])
                d[edge[j].a]=edge[j].w+d[edge[j].b];
            if(d[edge[j].b]>edge[j].w+d[edge[j].a])
                d[edge[j].b]=edge[j].w+d[edge[j].a];
        }
    }
    printf("%d\n",d[n]);
}


int main(){
    int a,b,c;
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&a,&b,&c);
        edge[i].a=a;
        edge[i].b=b;
        edge[i].w=c;
    } 
    bellman_ford();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值