ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-19 15:44:19  阅读:270  来源: 互联网

标签:map dis2 Cow cow dis1 int 源点 POJ party


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;
}

标签:map,dis2,Cow,cow,dis1,int,源点,POJ,party
来源: https://blog.csdn.net/qq_44293044/article/details/99730892

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有