Silver Cow Party(边取反最短路)

版权声明:希望各位巨巨多多赐教 https://blog.csdn.net/Techay/article/details/49232823

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: N, M, and 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.
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
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
一群牛到一个地方聚会,来回花最少的时间,问这些牛中花最多时间多少。
首先想到floyd,然后看数据范围就炸了,首先是可以找到他们去的最短时间,然后可以发现,这些是有向边,回去的路是从目的地到家,其实只要把边全部取反,就又是和前面一样,一起参加聚会的最短时间,然后把两个时间加起来就是要的总和了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int head[1005],d[1005],leap[1005],dis[1005];
struct Node
{
    int u,v,w,next;
}node[100005];
void add_edge(int u,int v,int w,int tot)
{
    node[tot].u = u;node[tot].v = v;
    node[tot].w = w;
    node[tot].next = head[u];
    head[u] = tot;
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int n,m,x;
    while(scanf("%d%d%d",&n,&m,&x) != EOF)
    {
        for(int i = 1;i <= n;i++)head[i] = -1;
        for(int i = 1;i <= m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add_edge(u,v,w,i);
        }
        for(int i = 1;i <= n;i++)d[i] = inf;
        for(int i = 1;i <= n;i++)leap[i] = 0;
        queue<int>p;
        p.push(x);
        leap[x] = 1;d[x] = 0;
        while(p.size())
        {
            int u = p.front();p.pop();
            leap[u] = 0;
            for(int i = head[u];i != -1;i = node[i].next)
            {
                int e = node[i].v;
                if(d[e] > d[u] + node[i].w)
                {
                    d[e] = d[u] + node[i].w;
                    if(!leap[e])
                    {
                        leap[e] = 1;p.push(e);
                    }
                }
            }
        }
        for(int i = 1;i <= n;i++)leap[i] = 0;
        for(int i = 1;i <= n;i++)head[i] = -1;
        for(int i = 1;i <= n;i++)dis[i] = inf;
        for(int i = 1;i <= m;i++)
        {
            add_edge(node[i].v,node[i].u,node[i].w,i);
        }
        p.push(x);
        dis[x] = 0;leap[x] = 1;
        while(p.size())
        {
            int u = p.front();p.pop();
            leap[u] = 0;
            for(int i = head[u];i != -1;i = node[i].next)
            {
                int e = node[i].v;
                if(dis[e] > dis[u] + node[i].w)
                {
                    dis[e] = dis[u] + node[i].w;
                    if(!leap[e])
                    {
                        leap[e] = 1;p.push(e);
                    }
                }
            }
        }
        int ans = -1;
        for(int i = 1;i <= n;i++)
            ans = max(ans,d[i] + dis[i]);
        printf("%d\n",ans);
    }
    return 0;
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页