POJ3268 (单源最短路径)

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.

解题:
每头牛返回的最短时间很简单就可以算出来,这相当于从目标牛为起点求单源最短路径。但每头牛出发到目标牛的最短时间无法直接算出来,稍微转换一下,发现这个最短时间其实可以通过把所有的边取反向,然后再从目标牛求一次单源最短路径得到。得到这两个最短路径之后,取它们的和的最大者即可。

附上大佬的代码
代码来自
原文:https://blog.csdn.net/alongela/article/details/8262595

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;
 
const int N = 1005;
int edge[N][N];
int n, e, x;
int mindis1[N];
int mindis2[N];
bool vis1[N];
bool vis2[N];
 
void init()
{
	for (int i = 0; i < n; ++i) 
	{
		for (int j = 0; j < n; ++j)
		{
			edge[i][j] = -1;
		}
	}
}
 
void dijkstra(int s)
{
	int pos1, pos2, te1, te2, tm1, tm2;
	for (int i = 0; i < n; ++i) 
	{
		vis1[i] = false;
		vis2[i] = false;
		mindis1[i] = 999999;
		mindis2[i] = 999999;
	}
	mindis1[s] = 0;
	mindis2[s] = 0;
	vis1[s] = true;
	vis2[s] = true;
	pos1 = s;
	pos2 = s;
	for (int i = 0; i < n - 1; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			if (!vis1[j] && edge[pos1][j] != -1 &&  mindis1[pos1] + edge[pos1][j] < mindis1[j])
			{
				mindis1[j] = mindis1[pos1] +edge[pos1][j];
			}
			if (!vis2[j] && edge[j][pos2] != -1 && mindis2[pos2] + edge[j][pos2] < mindis2[j])
			{
				mindis2[j] = mindis2[pos2] + edge[j][pos2];
			}
		}
		tm1 = 999999;
		tm2 = 999999;
		for (int j = 0; j < n; ++j)
		{
			if (!vis1[j] && mindis1[j] < tm1)
			{
				tm1 = mindis1[j];
				te1 = j;
			}
			if (!vis2[j] && mindis2[j] < tm2)
			{
				tm2 = mindis2[j];
				te2 = j;
			}
		}
		vis1[te1] = true;
		pos1 = te1;
		vis2[te2] = true;
		pos2 = te2;
	}
}
 
int main()
{
	int beg, end, dis;
	scanf("%d%d%d", &n, &e, &x);
	init();
	for (int i = 0; i < e; ++i)
	{
		scanf("%d%d%d", &beg, &end, &dis);
		--beg;
		--end;
		edge[beg][end] = dis;
	}
	dijkstra(--x);
	int ans = 0;
	for (int i = 0; i < n; ++i)
	{
		if (i == x) continue;
		if (mindis1[i] + mindis2[i] > ans) ans = mindis1[i] + mindis2[i];
	}
	printf("%d\n", ans);
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值