Silver Cow Party
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条路来连接这N个农场,及X表示举办party的农场求最短路的题,这个题主要是分为去和来两个过程,这就需要我们用两次Djikstra算法,求两次最小路,然后去加一下求去和的最大的,具体代码实现如下,反向回去的路要反向建图,然后就达到反向的目的
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int INF=0x3f3f3f3f;
int p[1005][1005];
int dis1[1005];
int dis2[1005];
int vis[1005];
int main()
{
int i,j,n,m,x,a,b,c,key,minn;
scanf("%d%d%d",&n,&m,&x);
for(i=1;i<=n;i++)
{
dis1[i]=INF;
dis2[i]=INF;
vis[i]=0;
for(j=1;j<=n;j++)
p[i][j]=INF;
}
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
p[a][b]=c;
}
dis1[x]=0;
key=x;
for(i=1;i<=n;i++)
{
minn=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&dis1[j]<minn)
{
minn=dis1[j];
key=j;
}
}
vis[key]=1;
for(j=1;j<=n;j++)
{
if(minn+p[key][j]<dis1[j])
dis1[j]=minn+p[key][j];
}
}
for(i=1;i<=n;i++)
{
vis[i]=0;
}
dis2[x]=0;
key=x;
for(i=1;i<=n;i++)
{
minn=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&dis2[j]<minn)
{
minn=dis2[j];
key=j;
}
}
vis[key]=1;
for(j=1;j<=n;j++)
{
if(minn+p[j][key]<dis2[j])
dis2[j]=minn+p[j][key];
}
}
minn=0;
for(i=1;i<=n;i++)
{
if(minn<dis1[i]+dis2[i])
minn=dis1[i]+dis2[i];
}
printf("%d\n",minn);
return 0;
}
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!