UVA 10806 - Dijkstra, Dijkstra.(费用流)

Problem ?
Dijkstra, Dijkstra.
Time Limit: 10 seconds

Dexter: "You don't understand. I can't walk...
they've tied my shoelaces together."

Topper Harley: "A knot. Bastards!"

Jim Abrahams and Pat Proft,
"Hot Shots! Part Deux."

You are a political prisoner in jail. Things are looking grim, but fortunately, your jailmate has come up with an escape plan. He has found a way for both of you to get out of the cell and run through the city to the train station, where you will leave the country. Your friend will escape first and run along the streets of the city to the train station. He will then call you from there on your cellphone (which somebody smuggled in to you inside a cake), and you will start to run to the same train station. When you meet your friend there, you will both board a train and be on your way to freedom.

Your friend will be running along the streets during the day, wearing his jail clothes, so people will notice. This is why you can not follow any of the same streets that your friend follows - the authorities may be waiting for you there. You have to pick a completely different path (although you may run across the same intersections as your friend).

What is the earliest time at which you and your friend can board a train?

Problem, in short
Given a weighed, undirected graph, find the shortest path from S to T and back without using the same edge twice.

Input
The input will contain several test cases. Each test case will begin with an integer n (2<=n<=100) - the number of nodes (intersections). The jail is at node number 1, and the train station is at node number n. The next line will contain an integer m - the number of streets. The next m lines will describe the m streets. Each line will contain 3 integers - the two nodes connected by the street and the time it takes to run the length of the street (in seconds). No street will be longer than 1000 or shorter than 1. Each street will connect two different nodes. No pair of nodes will be directly connected by more than one street. The last test case will be followed by a line containing zero.

Output
For each test case, output a single integer on a line by itself - the number of seconds you and your friend need between the time he leaves the jail cell and the time both of you board the train. (Assume that you do not need to wait for the train - they leave every second.) If there is no solution, print "Back to jail".

Sample InputSample Output
2
1
1 2 999
3
3
1 3 10
2 1 20
3 2 50
9
12
1 2 10
1 3 10
1 4 10
2 5 10
3 5 10
4 5 10
5 7 10
6 7 10
7 8 10
6 9 10
7 9 10
8 9 10
0
Back to jail
80
Back to jail

题意:找从1到n来回的最短路。每条边走一次

思路:最大流最小费用,建图,源点到1容量2费用0,n到汇点容量2,费用0,最后判断最大流如果为2则是可以到的,输出最小费用

代码:

#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
#include <queue>
using namespace std;
const int N = 105, M = 50005;

int n, m, A, B, D, K, E, first[N], next[M], u[M], v[M], pe[N], pv[N], a[N], f[M], w[M], s, t, value, cost[M];
queue<int>q;

void add(int a, int b, int value, int time) {
	u[E] = a; v[E] = b; w[E] = value; cost[E] = time;
	next[E] = first[u[E]];
	first[u[E]] = E ++;
	u[E] = b; v[E] = a; w[E] = 0; cost[E] = -time;
	next[E] = first[u[E]];
	first[u[E]] = E ++;
}

void init() {
	scanf("%d", &m);
	E = s = 0, t = n + 1;
	memset(first, -1, sizeof(first));
	for (int i = 0; i < m; i ++) {
		scanf("%d%d%d", &A, &B, &value);
		add(A, B, 1, value);
		add(B, A, 1, value);
	}
	add(s, 1, 2, 0);
	add(n, t, 2, 0);
}

void solve() {
	init();
	bool vis[N];
	int d[N], C = 0;
	int F = 0;
	memset(f, 0, sizeof(f));
	while(1) {
		memset(d, INF, sizeof(d));
		d[s] = 0;
		memset(vis, 0, sizeof(vis));
		q.push(s);
		while(!q.empty()) {
			int u = q.front(); q.pop();
			vis[u] = 0;
			for (int e = first[u]; e != -1; e = next[e]) {
				if (w[e] > f[e] && d[v[e]] > d[u] + cost[e]) {
					d[v[e]] = d[u] + cost[e];
					pv[v[e]] = u; pe[v[e]] = e;
					if (!vis[v[e]]) {
						vis[v[e]] = 1;
						q.push(v[e]);
					}
				}
			}
		}
		if (d[t] == INF) break;
		int a = INF;
		for (int v = t; v != s; v = pv[v])
			a = min(a, w[pe[v]] - f[pe[v]]);
		for (int v = t; v != s; v = pv[v]) {
			f[pe[v]] += a;
			f[pe[v]^1] -= a;
		}
		C += d[t] * a;
		F += a;
	}
	if (F < 2) printf("Back to jail\n");
	else printf("%d\n", C);
}

int main() {
	while (~scanf("%d", &n) && n) {
		solve();
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Dijkstra算法是一种用于解决单源最短路径问题的算法。它的基本思想是从起点开始,逐步扩展到其他节点,每次选择当前距离起点最近的节点,并更新与该节点相邻的节点的距离。通过这种方式,可以找到起点到其他节点的最短路径。Dijkstra算法的时间复杂度为O(n^2),但是可以通过使用堆优化来将其优化到O(nlogn)。 ### 回答2: Dijkstra算法是一种解决单源最短路径问题的贪心算法,其思想是利用“松弛”操作来不断更新当前点到源点的最短距离,但前提是所有边的权重非负。如果有负权边,则需要使用Bellman-Ford算法。 首先,我们需要定义一个数组dis数组,用于存储源点s到各个点的最短距离。dis[s]初始为0,其他点初始为无限大。接着,我们需要维护一个集合S,表示已经求出最短路径的点的集合。将源点s加入集合S中。 对于每个未加入S的点v,我们通过选择其它点到源点s的最短路径中的一个点u,然后将dis[v]更新为dis[u] + w(u,v),其中w(u,v)表示边(u,v)的权重。具体地,这个操作称为“松弛”操作。 在松弛操作中,我们需要比较dis[u] + w(u,v)和dis[v]的大小,如果前者更小,则更新dis[v]的值为dis[u] + w(u,v)。 重复执行以上操作,直到所有的点都加入到集合S中。最后dis数组中存储的就是源点s到所有点的最短距离。 Dijkstra算法可以用堆优化,时间复杂度为O(mlogn),其中n表示图中的点数,m表示边数。Dijkstra算法也可以应用于稠密图,时间复杂度为O(n^2)。 总之,Dijkstra算法是一种经典的求解单源最短路径问题的算法,其实现简单,效率高,被广泛应用于路由算法和图像处理等领域。 ### 回答3: Dijkstra算法是一种在加权有向图中寻找从源节点到其他节点的最短路径的贪心算法。该算法基于其它路径加权节点的已知最短路径去更新更长路径的信息直到找到从源节点到目标节点的最短路径。在整个计算过程中,Dijkstra算法需要维护一个待处理节点集合和一个距离源节点的最短路径数组。 算法的具体实现如下: 1. 初始化源节点及其距离为0,其他节点的距离为无穷大。 2. 将源节点加入到待处理节点集合中。 3. 对于源节点的所有相邻节点,更新它们距离源节点的最短路径。如果当前路径小于之前已知的最短路径,则更新最短路径数组。 4. 遍历待处理节点集合中除源节点外的节点,选择距离最近的节点作为当前节点,并将它从待处理机集合中移除。 5. 对于当前节点的所有相邻节点,更新它们距离源节点的最短路径。如果当前路径小于之前已知的最短路径,则更新最短路径数组。 6. 重复步骤4和5,直到待处理节点集合为空或者目标节点已经被遍历。 Dijkstra算法的时间复杂度为O(n^2),其中n为节点数,由于它是贪心算法,只能处理非负权重的图,否则可能会陷入死循环。但是,Dijkstra算法是单源最短路径问题的最优解,因此在处理小规模的图时效果很好。在处理大规模图时,需要使用其他高效的算法,如A*算法、Bellman-Ford算法等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值