POJ-----3268双向最短路

解决N头牛参加聚会的问题,通过构建有向图并利用两次最短路径算法找到最长的往返时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18581 Accepted: 8491

Description

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; 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头牛的住址,再给出一头牛要举办聚会,所有牛都得去,所有的路都是单向的,所以去的路和回的路不同,问所有牛来回最短路最大的那个是多少

由于图是有向图,画画图就能理解,从聚会点出来往回走,可以用最短路模板,而求去聚会的路上的时间时,依次遍历太耗时,我没试,但可以反着想,把终点和起点颠倒一下,把去看成回,把单向路箭头方向改变,再用一次模板,就可以一次求出去参加聚会的时间,然后取和最大的那个就行了


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 100010
#define inf 0x3f3f3f3f
using namespace std;
int m, n, ans, cnt, num1, num2;
int dis[maxn], head1[maxn], head2[maxn], dist[maxn];
bool vis[maxn];
struct node{
	int to, val, next;
}edge[maxn], Edge[maxn];
void add(int u, int v, int w){
	edge[num1].to = v;
	edge[num1].val = w;
	edge[num1].next = head1[u];
	head1[u] = num1++;
	Edge[num2].to = u;
	Edge[num2].val = w;
	Edge[num2].next = head2[v];
	head2[v] = num2++;
}
void bfs(int t){
	memset(dis, inf, sizeof(dis));
	memset(vis, false, sizeof(vis));
	queue<int >Q;
	dis[t] = 0;
	Q.push(t);
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		vis[u] = false;
		for(int i = head1[u]; i != -1; i = edge[i].next){
			int v = edge[i].to;
			if(dis[v] > dis[u] + edge[i].val){
				dis[v] = dis[u] + edge[i].val;
				if(!vis[v]){
					vis[v] = true;
					Q.push(v);
				}
			}
		}
	}
}
void bfs2(int t){
	memset(dis, inf, sizeof(dis));//手贱,写成了sizeof(inf)-----生无可恋.jpg
	memset(vis, false, sizeof(vis));
	queue<int >Q;
	dis[t] = 0;
	Q.push(t);
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		vis[u] = false;
		for(int i = head2[u]; i != -1; i = Edge[i].next){
			int v = Edge[i].to;
			if(dis[v] > dis[u] + Edge[i].val){
				dis[v] = dis[u] + Edge[i].val;
				if(!vis[v]){
					vis[v] = true;
					Q.push(v);
				}
			}
		}
	}
}
int main(){
	int a, b, c, x, y, z;
	scanf("%d%d%d", &x, &y, &z);
	num1 = num2 = 0;
	memset(head1, -1, sizeof(head1));
	memset(head2, -1, sizeof(head2));
	memset(dist, 0, sizeof(dist));
	for(int i = 0; i < y; i++){
		scanf("%d%d%d", &a, &b, &c);
		add(a, b, c);
	}
	bfs(z);
	for(int i = 1; i <= x; i++){
		if(i != z){
			dist[i] = dis[i];
		}
	}
	bfs2(z);
	for(int i = 1; i <= x; i++){
		if(i != z){
			dist[i] += dis[i];
		}
	}
	int ans = 0;
	for(int i = 1; i <= x; i++){
		if(i != z){
			if(ans < dist[i]){
				ans = dist[i];
			}
		}
	}
	cout<< ans <<endl;
	return 0;
}


Kruskal算法是一种用来求解小生成树的贪心算法。它的基本思想是,按照边的权值从小到大的顺序选择边,并且保证所选的边不会形成环,直到选取了n-1条边为止。另外,Kruskal算法还需要使用并查集来判断两个节点是否属于同一个连通分量。 在具体的实现过程中,可以按照以下步骤进行: 1. 将图中的所有边按照权值从小到大排序。 2. 创建一个并查集,并初始化每个节点为一个独立的集合。 3. 遍历排序后的边列表,对于每一条边(u, v),判断u和v是否属于同一个连通分量。如果不属于,则将这条边加入小生成树中,并将u和v合并到同一个连通分量中。 4. 重复步骤3,直到小生成树中的边数达到n-1。 通过以上步骤,就可以使用Kruskal算法求解出给定图的小生成树。 参考资料: 引用:(1)度限制小生成树和第K短路. (poj1639) (2)短路,小生成树,二分图,大流问题的相关理论(主要是模型建立和求解) (poj3155, poj2112,poj1966,poj3281,poj1087,poj2289,poj3216,poj2446 (3)优比率生成树. (poj2728) (4)小树形图(poj3164) (5)次小生成树. (6)无向图、有向图小环 。 引用:http://home.ustc.edu.cn/~zhuhcheng/ACM/segment_tree.pdf 。 引用:练习复杂一点,但也较常用的算法。 二分图匹配(匈牙利),小路径覆盖 网络流,小费用流。 线段树. 并查集。 熟悉动态规划的各个典型:LCS、长递增子串、三角剖分、记忆化dp 6.博弈类算法。博弈树,二进制法等。 7.大团,大独立集。 8.判断点在多边形内。 差分约束系统. 双向广度搜索、A*算法,小耗散优先. 第三阶段: 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值