[kuangbin]专题四 最短路练习 Silver Cow Party POJ - 3268【dijkstra】

【题目描述】
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1…N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
来自N个农场的牛(1≤N≤1000)被编号为1…N将参加在X农场(1≤X≤N)举行的聚会。总共M条(1≤M≤100000)单向(单程公路连接一对农场;道路i需要Ti(1≤Ti≤100)单位的时间穿越。
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow’s return route might be different from her original route to the party since roads are one-way.
每头母牛都必须走到派对上,等聚会结束后,再回到她的农场。每头牛都很懒,因此选择了一条最短时间的最佳路线。母牛的返回路线可能与她最初的派对路线不同,因为道路是单向的。
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
在所有的奶牛中,一头母牛要花多少时间才能走到派对上并且再回来呢?

【输入】
Line 1: Three space-separated integers, respectively: N, M, and X
第1行三个用空格隔开的整数N,M和X
Lines 2… M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
第2行到第M+1行中第i+1行表示道路i的三个用空格隔开的整数Ai,Bi和Ti。描述的道路从Ai到Bi,需要Ti的时间穿越。

【输出】
Line 1: One integer: the maximum of time any one cow must walk.
第一行一个整数表示一头母牛必须走的最长时间。

【样例输入】
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

【样例输出】
10

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

做法一:
跑n次dijkstra
O(n*(m+n)*log n)
代码如下:

#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
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][MAXN+10];
void dijkstra(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        dist[start][i]=INF;
        vis[i]=false;
    }
    priority_queue<Node> Q;
    dist[start][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[start][v]>dist[start][u]+cost)
            {
                dist[start][v]=dist[start][u]+cost;
                Q.push(Node(v,dist[start][v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int n,m,x;
    cin>>n>>m>>x;
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        addedge(a,b,c);
    }
    for(int i=1;i<=n;i++)
        dijkstra(n,i);
    int ans=0;
    for(int i=1;i<=n;i++)
        ans=max(ans,dist[i][x]+dist[x][i]);
    cout<<ans<<endl;
    return 0;
}

做法二:
换个思路跑2次dijkstra
O((m+n)*log n)
代码如下:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=1000;
static const int MAXM=1000000;
int u[MAXM+10],v[MAXM+10],w[MAXM+10];
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[2][MAXN+10];
void dijkstra(int n,int start,int k)
{
    for(int i=1;i<=n;i++)
    {
        dist[k][i]=INF;
        vis[i]=false;
    }
    priority_queue<Node> Q;
    dist[k][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[u][i].v;
            int cost=E[u][i].cost;
            if(!vis[v] && dist[k][v]>dist[k][u]+cost)
            {
                dist[k][v]=dist[k][u]+cost;
                Q.push(Node(v,dist[k][v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int n,m,x;
    cin>>n>>m>>x;
    for(int i=1;i<=m;i++)
    {
        cin>>u[i]>>v[i]>>w[i];
        addedge(u[i],v[i],w[i]);
    }
    dijkstra(n,x,0);
    for(int i=1;i<=n;i++)
        E[i].clear();
    for(int i=1;i<=m;i++)
        addedge(v[i],u[i],w[i]);
    dijkstra(n,x,1);
    int ans=0;
    for(int i=1;i<=n;i++)
        ans=max(ans,dist[0][i]+dist[1][i]);
    cout<<ans<<endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值