题目来源:

https://vjudge.net/contest/290751#problem/D

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: 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个农场中,指定一个农场x,剩余的农场要有牛来到农场x参加Party,每只牛来到农场x会走最短的路径,返回的路径不一定按原路返回,因为每一条路都是单向的。返回也走最短路径。求N - 1 只牛去参加Party到返回自己农场的最短路径和中的最大值。

思路:

简单的最短路径,一开始用了floyd算法,超时了,N最大达到了1000,N^3的复杂度,铁定的超时。。。

后来想了一下从i到j的最短路不就是邻接矩阵转置之后b到a的的最短路吗,这样问题就简单了,跑两遍dijstra就ok了

floyd的超时代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#define inf 0x3f3f3f3f
#define N 1005
#define eps 1e-5
using namespace std;
int  s[N],dis[N][N],G[N][N],n,m,x;
void floyd()
{
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            dis[i][j]=G[i][j];
        }
    }
    for(int k=1; k<=n; k++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(dis[i][j]>dis[i][k]+dis[k][j])
                {
                    dis[i][j]=dis[i][k]+dis[k][j];
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&x))
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                G[i][j]=inf;
            }
            G[i][i]=0;
        }
        while(m--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            G[a][b]=c;
        }
        floyd();
        for(int i=1; i<=n; i++)
        {
            s[i]=dis[i][x]+dis[x][i];
        }
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            if(s[i]>ans)
            {
                ans=s[i];
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

两遍dijstra的ac代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#define inf 0x3f3f3f3f
#define N 1005
#define eps 1e-5
using namespace std;
int  s[N],dis[N][N],G[N][N],n,m,x,G2[N][N];
bool vis[N];
void dijstra(int *dis,int G[N][N])
{
	memset(vis,0,sizeof(vis));
	dis[x]=0;
	while(1)
	{
		int k=-1;
		int Min=inf;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]==0 && dis[i]<Min)
			{
				Min=dis[i];
				k=i;
			}
		}
		if(k==-1) break;
		vis[k]=1;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]==0 && dis[i]>dis[k]+G[k][i])
			{
				dis[i]=dis[k]+G[k][i];
			}
		}
	}
}
int main()
{
	while(~scanf("%d%d%d",&n,&m,&x))
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				G[i][j]=inf;
			}
			G[i][i]=0;
		}
		while(m--)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			G[a][b]=c;
		}
		int dis1[N];
		for(int i=1;i<=n;i++)dis1[i]=inf;
		dijstra(dis1,G);
		int dis2[N];
		for(int i=1;i<=n;i++)dis2[i]=inf;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				G2[i][j]=G[j][i];
			}
		}
		dijstra(dis2,G2);
		int s[N];
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			s[i]=dis1[i]+dis2[i];
			ans=max(ans,s[i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}