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:  NM, 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的农场)哪个农场的牛走的最短路最长。

       我是初学最短路,开始求的最短路都是面对一对一的,现在是多对一,每个都求肯定超时,不知道有多少初学者都栽了,反正我栽了,所以这肯定是不可靠的。比较了解最短路后我们可以知道,其实每个求完最短路后,dis数组中存储的就是从起点到其他每个点的最短路了,所以上面这道题的回去的问题就已经解决了,那么来的呢? ,但是我把置换的结果打印出来就发现了

 

结果如上图所示,我是给的样例的数据,所以置换之后,比如4到2的换成了2到4,1到2就改成了2到1,这样我们就可以又求一次以2为起点到其他各点的距离这样就求出了来参加时的最短路,最后从1到n遍历取来回最短路之和最大的那个就是正确答案。

因为个人对这些不是很熟,一个小小的转置不解释一下确实不懂,所以在这里写出来了。接下来是代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAX 0x3f3f3f3f
int map[1005][1005];
int main()
{
	int n, m, k, i, j, vis[1005], dis[1005], a, b, len, x, min, dis1[1005];
	scanf("%d%d%d", &n, &m, &x);
	memset(map, 0, sizeof(map));
	memset(vis, 0, sizeof(vis));
	memset(dis, 0, sizeof(dis));
	while (m--)
	{
		scanf("%d%d%d", &a, &b, &len);
		if (!map[a][b] || map[a][b] > len)
		{
			map[a][b] = len;
		}
	}
	for (i = 1; i <= n; i++)
	{
		dis[i] = map[x][i];
	}
	vis[x] = 1;
	for (i = 1; i <= n; i++)
	{
		min = MAX;
		for (j = 1; j <= n; j++)
		{
			if (!vis[j] && dis[j] && dis[j] < min)
			{
				min = dis[j];
				k = j;
			}
		}
		if (min == MAX)
		{
			break;
		}
		vis[k] = 1;
		for (j = 1; j <= n; j++)
		{
			if (!vis[j] && map[k][j] && (map[k][j] + dis[k] < dis[j] || !dis[j]))
			{
				dis[j] = map[k][j] + dis[k];
			}
		}
	}
	for (i = 1; i <= n; i++)
	{
		for (j = i + 1; j <= n; j++)
		{
			int t = map[i][j];
			map[i][j] = map[j][i];
			map[j][i] = t;
		}
	}
	memset(vis, 0, sizeof(vis));
	for (i = 1; i <= n; i++)
	{
		dis1[i] = map[x][i];
	}
	vis[x] = 1;
	for (i = 1; i <= n; i++)
	{
		min = MAX;
		for (j = 1; j <= n; j++)
		{
			if (!vis[j] && dis1[j] && dis1[j] < min)
			{
				min = dis1[j];
				k = j;
			}
		}
		if (min == MAX)
		{
			break;
		}
		vis[k] = 1;
		for (j = 1; j <= n; j++)
		{
			if (!vis[j] && map[k][j] && (map[k][j] + dis1[k] < dis1[j] || !dis1[j]))
			{
				dis1[j] = map[k][j] + dis1[k];
			}
		}
	}
	int sum = 0;
	for (i = 1; i <= n; i++)
	{
		if (i != x)
		{
			sum = max(sum, dis[i] + dis1[i]);
		}
	}
	printf("%d\n", sum);
	return 0;
}

因为两次最短路处理都是一样的,可以自己定义一个调用函数让代码更简洁一些,我这里给出我最先AC的代码。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值