码迷,mamicode.com
首页 > 其他好文 > 详细

Silver Cow Party题解

时间:2020-07-10 13:33:46      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:algorithm   define   proc   要求   scribe   i+1   via   strong   clu   

Silver Cow Party

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.

题意

给出N个点M条边的有向连通图和一个指定终点X,求所有点走最短路往返X一次路径之和的最大值。

思路

明显的最短路问题,往返都要求一次最短路,由于是有向图,这两条路径可能不相同。于是考虑建两次图,正图反图各一次,起点设置为X各跑一次spfa,转化为单源最短路径问题:正图从X出发求返回的最短路径,反图从X出发求前往的最短路径,最后各点两个dis之和中的最大值即为答案。最坏复杂度O(2 * N * M),时限两秒,Accepted。

Code

#include<iostream>
#include<algorithm>
#include<queue>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int maxn=1050;
const int maxm=100050;
int n,m,x;
int tot,V[maxm],Nxt[maxm],Head[maxn],W[maxm],dis1[maxn],vis[maxn];
int tott,V1[maxm],Nxt1[maxm],Head1[maxn],W1[maxm],dis2[maxn],vis1[maxn];
void Add(int u,int v,int w){V[++tot]=v;W[tot]=w;Nxt[tot]=Head[u];Head[u]=tot;}
void Add1(int u,int v,int w){V1[++tott]=v;W1[tott]=w;Nxt1[tott]=Head1[u];Head1[u]=tott;}
void spfa()
{
    queue<int>q;
    for(int i=1;i<=n;i++)dis1[i]=1e8;
    dis1[x]=0;vis[x]=1;q.push(x);
    while(!q.empty())
    {
        int u=q.front();q.pop();vis[u]=0;
        for(int i=Head[u];i;i=Nxt[i])
        {
            int v=V[i],w=W[i];
            if(dis1[v]>dis1[u]+w)
            {
                dis1[v]=dis1[u]+w;
                if(!vis[v])q.push(v),vis[v]=1;
            }
        }
    }
}
void spfa1()
{
    queue<int>q1;
    for(int i=1;i<=n;i++)dis2[i]=1e8;
    dis2[x]=0;vis1[x]=1;q1.push(x);
    while(!q1.empty())
    {
        int u=q1.front();q1.pop();vis1[u]=0;
        for(int i=Head1[u];i;i=Nxt1[i])
        {
            int v=V1[i],w=W1[i];
            if(dis2[v]>dis2[u]+w)
            {
                dis2[v]=dis2[u]+w;
                if(!vis1[v])q1.push(v),vis1[v]=1;
            }
        }
    }
}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	IO;
    cin>>n>>m>>x;
    for(int i=1,x,y,z;i<=m;i++)cin>>x>>y>>z,Add(x,y,z),Add1(y,x,z);
    spfa();spfa1();
    int maxx=0;
    for(int i=1;i<=n;i++)maxx=max(maxx,dis1[i]+dis2[i]);
    cout<<maxx;
    return 0;
}

Silver Cow Party题解

标签:algorithm   define   proc   要求   scribe   i+1   via   strong   clu   

原文地址:https://www.cnblogs.com/-Dominate-/p/13278532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!