[kuangbin]专题四 最短路练习 Til the Cows Come Home POJ - 2387【dijkstra/bellman_ford/spfa】

【题目描述】
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.
Bessie在田里,想回到谷仓,在农夫John叫醒她早上挤奶之前,尽可能多地睡一觉。Bessi需要她的美容觉,所以她想尽快回来。
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.
农夫John有N(2 <= N <= 1000)块田,被标记为1到n。地标1是谷仓,苹果树林在Bessie每天在的N处,奶牛在田野中行走时有T(1<=T<=2000)条双向路径,在地标之间有不同的长度。Bessie对自己的导航能力没有信心,所以一旦她开始航行,她就会一直保持从一开始到最后的轨迹。
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.
考虑到地标之间的小径,确定Bessie必须步行的最小距离回到谷仓。保证存在这样的路线。

【输入】
* Line 1: Two integers: T and N
第1行两个整数T和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.
第2行到第T+1行每行三个用空格分隔的整数。前两个整数是轨迹之间的地标。第三个整数是路的长度,在1到100之间

【输出】
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
第一行一个整数表示从N到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.

题目链接:https://cn.vjudge.net/problem/POJ-2387

模板题

代码如下:

dijkstra O(n2)

#include <iostream>
#include <cstring>
using namespace std;
static const int MAXN=1000;
int E[MAXN+10][MAXN+10],d[MAXN+10],n;
bool vis[MAXN+10];
void dijkstra()
{
    memset(d,0x3f,sizeof(d));
    memset(vis,false,sizeof(vis));
    d[1]=0;
    for(int i=1;i<n;i++)
    {
        int x=0;
        for(int j=1;j<=n;j++)
            if(!vis[j] && (x==0 || d[j]<d[x]))
                x=j;
        vis[x]=true;
        for(int y=1;y<=n;y++)
            d[y]=min(d[y],d[x]+E[x][y]);
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T;
    cin>>T>>n;
    memset(E,0x3f,sizeof(E));
    for(int i=1;i<=n;i++)
        E[i][i]=0;
    for(int i=1;i<=T;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        E[a][b]=min(E[a][b],c);
        E[b][a]=E[a][b];
    }
    dijkstra();
    cout<<d[n]<<endl;
    return 0;
}

dijkstra堆优化 O((n+m)log n)

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=1000;
struct Node{
    int v,c;
    Node(int _v=0,int _c=0):v(_v),c(_c){};
    bool operator <(const Node &r)const{
        return c>r.c;
    }
};
struct Edge{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){};
};
vector<Edge> E[MAXN+10];
bool vis[MAXN+10];
int dist[MAXN+10];
void dijkstra(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dist[i]=INF;
    }
    priority_queue<Node> Q;
    while(!Q.empty()) Q.pop();
    dist[start]=0;
    Q.push(Node(start,0));
    while(!Q.empty())
    {
        Node tmp=Q.top();
        Q.pop();
        int u=tmp.v;
        if(vis[u])
            continue;
        vis[u]=true;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[tmp.v][i].v;
            int cost=E[u][i].cost;
            if(!vis[v] && dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                Q.push(Node(v,dist[v]));
            }
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T,n;
    cin>>T>>n;
    for(int i=1;i<=T;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        E[u].push_back(Edge(v,w));
        E[v].push_back(Edge(u,w));
    }
    dijkstra(n,1);
    cout<<dist[n]<<endl;
    return 0;
}

bellman_ford O(n*m)

#include <iostream>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=1000;
int dist[MAXN+10];
struct Edge{
    int u,v,cost;
    Edge(int _u=0,int _v=0,int _cost=0):u(_u),v(_v),cost(_cost){}
};
vector<Edge> E;
void bellman_ford(int n,int start)
{
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    dist[start]=0;
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<E.size();j++)
        {
            int u=E[j].u;
            int v=E[j].v;
            int cost=E[j].cost;
            if(dist[v]>dist[u]+cost)
                dist[v]=dist[u]+cost;
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T,n;
    cin>>T>>n;
    for(int i=1;i<=T;i++)
    {
        int u,v,cost;
        cin>>u>>v>>cost;
        E.push_back(Edge(u,v,cost));
        E.push_back(Edge(v,u,cost));
    }
    bellman_ford(n,1);
    cout<<dist[n]<<endl;
    return 0;
}

spfa O(km)

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=1000;
struct Edge{
    int v,cost;
    Edge(int _v,int _cost):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN+10];
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
bool vis[MAXN+10];
int cnt[MAXN+10],dist[MAXN+10];
void spfa(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dist[i]=INF;
    }
    vis[start]=true;
    dist[start]=0;
    queue<int> Q;
    while(!Q.empty())
        Q.pop();
    Q.push(start);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        vis[u]=false;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            int cost=E[u][i].cost;
            if(dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                if(!vis[v])
                {
                    vis[v]=true;
                    Q.push(v);
                }
            }
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T,n;
    cin>>T>>n;
    for(int i=1;i<=T;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        addedge(u,v,w);
        addedge(v,u,w);
    }
    spfa(n,1);
    cout<<dist[n]<<endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值