poj3268 Dijkstra算法 处理往返路问题

38 篇文章 0 订阅
9 篇文章 0 订阅

Description

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.

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?

Input

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

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

Sample Output

10

有N头牛要去参加牛X那里的聚会,现在除了X牛外,其他N-1头牛都要走到X牛那里去.给你M条有向边,现在问你任意一头牛从自己的位置走到X牛那,然后再走回来(来回都选择最短路径走)的话,需要的总时间的最大值是多少?即从所有N-1头牛中找那个最大的来回时间.

思路:

  首先本题的有向图,所以从X点到其他所有点的最短距离就是所有牛回家需要走的路.

然后把所有的边方向反过来,那么x点到其他点的最短距离就是所有牛来时所要走的路。

然后找打一头牛往返的最长时间就是答案。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=10000;
const int inf=0x3ffffff;
struct Edge
{
    int from,to,dist;
    Edge(){}
    Edge(int f,int t,int d):from(f),to(t),dist(d){}

};
struct Head
{
    int d;int u;
   // Head(){}
    Head(int dd,int uu):d(dd),u(uu){}
   bool operator<(const Head& ff)  const
    {
        return d>ff.d;
    }
};
struct dislal
{
    int n,m;
    vector<Edge> edges;
    vector<int> g[maxn];
    bool done[maxn];
    int d[maxn];
    void intn(int n)
    {
        this->n=n;
        for(int i=0;i<n;i++) g[i].clear();
        edges.clear();
    }
    void addedge(int from,int t,int d)
    {
        edges.push_back(Edge(from,t,d));
        int m=edges.size();
        g[from].push_back(m-1);
    }
    void dij(int s)
    {
         priority_queue<Head> Q;
        for(int i=0;i<n;i++) d[i]=inf;
        d[s]=0;
        memset(done,0,sizeof(done));
        Q.push(Head(0,s));
        while(!Q.empty())
        {
            //Head p=Q.;  Q.pop();

            Head p=Q.top(); Q.pop();
            int x=p.u;
            if(done[x])continue;
            done[x]=1;
            for(int i=0;i<g[x].size();i++)
            {
                Edge &e=edges[g[x][i]];
                if(d[e.to]>d[x]+e.dist)
                {
                    d[e.to]=d[x]+e.dist;
                    Q.push(Head(d[e.to],e.to));
                }
            }
        }

    }
}dj1,dj2;
int main()
{
    int n,m,s;
    cin>>n>>m>>s;
    s--;
    dj1.intn(n);
     dj2.intn(n);
     int x,y,w;
     for(int i=1;i<=m;i++)
     {
         cin>>x>>y>>w;
         x--;
         y--;
         dj1.addedge(x,y,w);
         dj2.addedge(y,x,w);
     }
     dj1.dij(s);
     dj2.dij(s);
     int maxt=-1;
     for(int i=0;i<n;i++)
     {
         maxt=max(maxt,(dj1.d[i]+dj2.d[i]));
     }
     cout<<maxt;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值