Silver Cow Party POJ - 3268(找各个点到源点来回路程最短中的最大值)

POJ—3268

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条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出。
思路:找来回路程的最短路,最后比较一下输出最大值即可,具体看代码,

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int map[1005][1005], dis1[1005], dis2[1005], vis[1005];
int n, m, x;

void dijstra2()//源点x到各点的最短路
{
	for (int i = 1; i <= n; i++)
	{
		dis2[i] = (i == x) ? 0 : map[x][i];
		vis[i] = 0;
	}
	vis[x] = 1;
	for (int i = 1; i <= n; i++)
	{
		int s = 0, minn = 9999999;
		for (int j = 1; j <= n; j++)
		{
			if (!vis[j] && minn > dis2[j])
			{
				minn = dis2[j];
				s = j;
			}
		}
		vis[s] = 1;
		for (int j = 1; j <= n; j++)
		{
			if (!vis[j] && dis2[j] > dis2[s] + map[s][j])
				dis2[j] = dis2[s] + map[s][j];
		}
	}
}

void dijstra1()//各点到源点x的最短路,
{
	for (int i = 1; i <= n; i++)
	{
		dis1[i] = (i == x) ? 0 : map[i][x];
		vis[i] = 0;
	}
	vis[x] = 1;
	for (int i = 1; i <= n; i++)
	{
		int s = 0, minn = 9999999;
		for (int j = 1; j <= n; j++)
		{
			if (!vis[j] && minn > dis1[j])
			{
				minn = dis1[j];
				s = j;
			}
		}
		vis[s] = 1;
		for (int j = 1; j <= n; j++)
		{
			if (!vis[j] && dis1[j] > dis1[s] + map[j][s])
				dis1[j] = dis1[s] + map[j][s];
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	while (cin >> n >> m >> x)
	{
		if (!m || !n || !x)
			break;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (i == j)
					map[i][j] = 0;
				else
					map[i][j] = map[j][i] = 9999999;
			}
		}
		for (int i = 1; i <= m; i++)
		{
			int s, e, p;
			cin >> s >> e >> p;
			if (map[s][e] > p)
				map[s][e] = p;
		}
		dijstra1();
		dijstra2();
		int ans = 0;
		for (int i = 1; i <= n; i++)
			ans = max(ans, dis1[i] + dis2[i]);
		cout << ans << endl;
	}
	return 0;
}
上一篇:经典二维数组查找算法


下一篇:D - Silver Cow Party