POJ 2387-Til the Cows Come Home(Dijkstra+堆优化)

Til the Cows Come Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40039 Accepted: 13620

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


题目意思:

有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离。


两种解题思路:

①Dijkstra;

②使用STL的priority_queue实现。


/*
* Copyright (c) 2016, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:dijkstra.cpp
* 作    者:单昕昕
* 完成日期:2016年3月30日
* 版 本 号:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 1010
#define INF 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
using namespace std;
int cost[MAXN][MAXN],dis[MAXN],n,t;
bool used[MAXN];//标识是否使用过
void dijkstra(int s)
{
    memset(used,0,sizeof(used));
    fill(dis,dis+n+1,INF);
    fill(used,used+n+1,false);
    dis[s]=0;
    while(true)
    {
        int v=-1;
        //从未使用过的顶点中选择一个距离最小的顶点
        for(int u=0; u<n; ++u)
            if(!used[u]&&(v==-1||dis[u]<dis[v]))
                v=u;
        if(v==-1) break;
        used[v]=true;
        for(int u=0; u<=n; ++u)
            dis[u]=min(dis[u],dis[v]+cost[v][u]);
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=0; i<=n; ++i)
            for(int j=0; j<=n; ++j)
                cost[i][j]=INF;//手动初始化,不能fill了……
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);
            if(l<cost[a][b])
                cost[a][b]=cost[b][a]=l;//无向图
        }
        dijkstra(1);
        printf("%d\n",dis[n]);
    }
    return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/


②使用STL的priority_queue实现。

内存占用少:

/*
* Copyright (c) 2016, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:queue.cpp
* 作    者:单昕昕
* 完成日期:2016年6月1日
* 版 本 号:v2.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
#define MAXN 1010
using namespace std;
int dis[MAXN],n,t;
struct edge
{
    int to;
    int cost;
};
vector<edge> G[MAXN];//边表
typedef pair<int,int> P;
//first是最短距离,second是顶点的编号
void dijkstra(int s)
{
    //通过指定greater<P>参数,堆按照first从大到小的顺序取初值
    priority_queue<P,vector<P>,greater<P> > que;
    fill(dis,dis+n+1,INF);//初始化范围要到n
    dis[s]=0;
    que.push(P(0,s));
    while(!que.empty())
    {
        P p=que.top();
        que.pop();
        int v=p.second;
        if(dis[v]<p.first) continue;
        for(int i=0; i<G[v].size(); ++i)
        {
            edge e=G[v][i];
            if(dis[e.to]>dis[v]+e.cost)//Dijkstra
            {
                dis[e.to]=dis[v]+e.cost;
                que.push(P(dis[e.to],e.to));
            }
        }
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);//a->b的距离是l
            G[a].push_back({b,l});//这里的语法要注意
            G[b].push_back({a,l});
        }
        dijkstra(1);//起点是1
        printf("%d\n",dis[n]);
    }
    return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/


/*
* Copyright (c) 2016, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:dijkstra.cpp
* 作    者:单昕昕
* 完成日期:2016年3月30日
* 版 本 号:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define inf 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
using namespace std;
int p[1003][1003],flag[1003],dis[1003],mmin,n,t;
void dijkstra()
{
    memset(flag,0,sizeof(flag));
    for(int i=2; i<=n; i++)
    {
        dis[i]=p[1][i];
    }
    flag[1]=1;
    int pre;
    for(int i=1; i<=n; i++)
    {
        mmin=inf;
        for(int j=1; j<=n; j++)
        {
            if(flag[j]==0&&dis[j]<mmin)
            {
                mmin=dis[j];
                pre=j;
            }
        }
        if(mmin==inf)
            break;
        flag[pre]=1;
        for(int j=1; j<=n; j++) //找最短路
        {
            if(flag[j]==0&&dis[pre]+p[pre][j]<dis[j])
            {
                dis[j]=dis[pre]+p[pre][j];
            }
        }
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                p[i][j]=inf;
            }
        }
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);
            if(l<p[a][b])
            {
                p[a][b]=p[b][a]=l;//无向图
            }
        }
        dijkstra();
        printf("%d\n",dis[n]);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值