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?
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.
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
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目标
给出每只牛之间的距离,每头牛都要渠道目标位置,然后再回来,求一个路径的最大值
思路:最短路问题
去的时候一种方案,回来之后起始位置交换,所以方案可能会有所不同,套上模板,求出来回的距离,相加求最大
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m,x;
int vis[1001];
int a[1001];
int b[1001];
int cost[1001][1001];
void solve(int dist[])
{
for(int i=1;i<=n;i++)
{
dist[i]=1000000;
vis[i]=0;
}
dist[x]=0;
for(int j=0;j<n;j++)
{
int k=-1;
int min=1000000;
for(int i=1;i<=n;i++)
if(!vis[i]&&dist[i]<min)
{
min=dist[i];
k=i;
}
if(k==-1)break;
vis[k]=1;
for(int i=1;i<=n;i++)
if(!vis[i]&&dist[k]+cost[k][i]<dist[i])
dist[i]=dist[k]+cost[k][i];
}
}
int main()
{
int i,j;
int ans;
while(cin>>n>>m>>x)
{
ans=-1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j)
cost[i][j]=0;
else
cost[i][j]=1000000;
}
int s,e,d;
for(i=0;i<m;i++)
{
cin>>s>>e>>d;
cost[s][e]=min(cost[s][e],d);
}
solve(a);
for(i=1;i<=n;i++)
for(j=1;j<i;j++)
swap(cost[i][j],cost[j][i]);
solve(b);
for(i=1;i<=n;i++)
ans=max(ans,a[i]+b[i]);
cout<<ans<<endl;
}
return 0;
}