您的位置:首页 > 其它

Silver Cow Party poj 3268

2015-08-03 14:28 316 查看
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.

(1)题意:来自N个农场的牛去参加一个party,party位于X号农场,牛去party后还要回农场(回去的路径可以不同并且来回的路都是时间最短的),求这些牛来回所走的最长时间。

(2)解法:用 Dijkstra1算出X点到各点的最短距离,在把矩阵倒置,再求X点到各点的最短距离,这里就实际是各点到X的最短距离了。两次的最短距离相加,求得最大的即结果。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 10000100
int value1[1005][1005];
int value2[1005][1005];
int d1[1005];
int d2[1005];

bool used1[1005];
bool used2[1005];
int m;
void nnn1(int s)
{
fill(d1+1,d1+m+1,INF);
fill(used1+1,used1+m+1,false);
d1[s]=0;
while(true)
{
int v=-1;
for(int u=1;u<=m;u++)
{
if(!used1[u]&&(v==-1||d1[u]<d1[v])) v=u;
}
if(v==-1) break;
used1[v]=true;
for(int u=1;u<=m;u++)
{
d1[u]=min(d1[u],d1[v]+value1[v][u]);
}
}
}
void nnn2(int s)
{
fill(d2+1,d2+m+1,INF);
fill(used2+1,used2+m+1,false);
d2[s]=0;
while(true)
{
int v=-1;
for(int u=1;u<=m;u++)
{
if(!used2[u]&&(v==-1||d2[u]<d2[v])) v=u;
}
if(v==-1) break;
used2[v]=true;
for(int u=1;u<=m;u++)
{
d2[u]=min(d2[u],d2[v]+value2[v][u]);
}
}
}
int main()
{
int n,x;
while(scanf("%d %d %d",&m,&n,&x)!=EOF)
{
int i,j;
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)
{
if(i==j)
{
value1[i][j]=0;
value2[j][i]=0;
}
else
{
value1[i][j]=INF;
value2[j][i]=INF;
}
}
int qi,zo,zhi;
for(i=1;i<=n;i++)
{
scanf("%d %d %d",&qi,&zo,&zhi);
value1[qi][zo]=zhi;
value2[zo][qi]=zhi;
}
nnn1(x);
nnn2(x);
int p=-1;
for(i=1;i<=m;i++)
{
p=max(p,d1[i]+d2[i]);
}
printf("%d\n",p);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: