您的位置:首页 > 其它

poj Silver Cow Party 3268 (来回单向最短路)好题

2016-03-27 21:13 411 查看
Silver Cow Party

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16996 Accepted: 7756
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头牛要去第x头牛的住处参加party。。参加完还要返回。。
先输入n,m,x;分别表示有n头牛,m条路单向,单向的,单向的路(重要的说三遍),x表示要去的终点。因为牛都比较懒,所以他们都会采取最短路的方式去和返回,现在问这n头牛中要花费最长时间的牛的时间。
//思路:
来回单向最短路,因为n的范围是(n<1000),所以用笛杰斯特拉就行了。平常我们写的都是去的,这块就要新学到点东西了,这里直接求得来回的最短路(很机智)。
具体看代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN__int64
#define N 1010
#define M 1000000007
using namespace std;
int map

;
int a
;
int vis
,dis
;//回去的最短距离
int vis1
,dis1
;//去的时候的最短距离
int n,m;
int djs(int y)
{
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
memset(vis1,0,sizeof(vis1));
memset(dis1,INF,sizeof(dis1));
dis[y]=0;dis1[y]=0;
int i,j,k,k1;
for(j=1;j<=n;j++)
{
k=-1;k1=-1;
for(i=1;i<=n;i++)
{
if(!vis[i]&&(k==-1||dis[i]<dis[k]))
k=i;
if(!vis1[i]&&(k1==-1||dis1[i]<dis1[k1]))
k1=i;
}
vis[k]=1;vis1[k1]=1;
for(i=1;i<=n;i++)
{
dis[i]=min(dis[i],dis[k]+map[k][i]);//回去
dis1[i]=min(dis1[i],dis1[k1]+map[i][k1]);//来
}
}
}
int main()
{
int i,j,k,x;
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
int u,v,w;
memset(map,INF,sizeof(map));
memset(a,0,sizeof(a));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(map[u][v]>w)
map[u][v]=w;
}
djs(x);
int mm=0;
for(i=1;i<=n;i++)
{
if(i==x)
continue;
mm=max(mm,dis[i]+dis1[i]);
}
printf("%d\n",mm);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: