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 ≤XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi requiresTi (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个农场,编号1~N,农场里奶牛将去X号农场。这N个农场之间有M条单向路(注意),通过第i条路将需要花费Ti单位时间。 选择最短时间的最优路径来回一趟, 花费在去的路上和返回农场的这些最优路径的最长时间是多少?
思路:计算出每头牛去X并且回来的最短路径所需要的时间,然后求出这n-1个农场的牛的最长时间即可, 两次运用dijkstra;
1 计算回来的时间:以X为源点,求出源点到各个农场的最短路径;
2 计算去的时间:将路径反转,在用一次dij,求源点到农场的最短路径(实则求牛去X的最短路径);
3 取两次和求最大值;

本题类似于 次小生成树----hpu王小二,都是同样的程序跑了两次,异曲同工吧;

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int graph[1010][1010],lowcost[1010],low[1010],mst[1010];
int x,n,m;
void dij(int v)//dijkstra模板;
{
	int i,j,min,minid;
	memset(mst,0,sizeof(mst));
	mst[v]=1;
	for(i=1;i<=n;i++)
		lowcost[i]=graph[v][i];
	for(i=2;i<=n;i++)
	{
		min=INF;
		for(j=1;j<=n;j++)
		{
			if(!mst[j] && lowcost[j]<min)
			{
				min=lowcost[j];
				minid=j;
			}
		}
		mst[minid]=1;
		for(j=1;j<=n;j++)
		{
			if(!mst[j] && lowcost[j]>graph[minid][j]+lowcost[minid])
				lowcost[j]=graph[minid][j]+lowcost[minid];
		} 
	}
}
int main()
{
	int i,j,time,wa;
	while(scanf("%d %d %d",&n,&m,&x)!=EOF)
	{
		memset(graph,INF,sizeof(graph));
		while(m--)
		{
			scanf("%d %d %d",&i,&j,&time);
			if(time<graph[i][j])
				graph[i][j]=time;//单向路径; 
		}
		dij(x);//计算牛回去的最短时间; 
		for(i=1;i<=n;i++)//执行完dij,lowcost[i]代表以x为起点,i为终点的最短路径;
			low[i]=lowcost[i];
		for(i=1;i<=n;i++)
		{
			for(j=i+1;j<=n;j++)//j不能从1开始,否则反转两次,相当于没变化; 
			{
				wa=graph[j][i];
				graph[j][i]=graph[i][j];
				graph[i][j]=wa;
			}
		}
		dij(x);
		int maxn=0,ans;
		for(i=1;i<=n;i++)
		{
			if(i!=x)//此条件不可少; 
				ans=low[i]+lowcost[i];//来回时间相加; 
			maxn=max(maxn,ans);
		}
		printf("%d\n",maxn);
	}
	return 0;
}

bellman-ford,思路一样,只是换种方式,和dijkstra 时间差不多;

#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
int mst[300000],mst1[300000];
int n,m,x;
struct node
{
	int a;
	int b;
	int c;
}s[300000];
void bellman_ford(int v)
{
	memset(mst,INF,sizeof(mst));
	mst[v]=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(mst[s[j].a]>s[j].c+mst[s[j].b])//单向路径;
				mst[s[j].a]=s[j].c+mst[s[j].b];
		}
	}
}
int main()
{
	int a,b,c,wa;
	while(scanf("%d %d %d",&n,&m,&x)!=EOF)
	{
		for(int i=1;i<=m;i++)
			scanf("%d %d %d",&s[i].a,&s[i].b,&s[i].c);
		bellman_ford(x);
		for(int i=1;i<=n;i++)//去的最短路径;
			mst1[i]=mst[i];
		for(int i=1;i<=m;i++)//置换一下;
		{
			wa=s[i].a;
			s[i].a=s[i].b;
			s[i].b=wa;
		}
		bellman_ford(x);//计算回来的最短路径;
		int ans,maxn=0;
		for(int i=1;i<=n;i++)
		{
			if(i!=x)
				ans=mst[i]+mst1[i];
			if(ans>maxn) maxn=ans;
		}
		printf("%d\n",maxn);
	}
	return 0;
} 


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值