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

原创 2013年12月05日 20:59:12

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


UVa 10806 Dijkstra, Dijkstra (无向图+最小费用最大流+最短路:)

参考:http://www.cnblogs.com/scau20110726/archive/2012/12/09/2810654.html 题意:      固定起点1和终点n,从1到n,再从n回到...
  • Slow_Wakler
  • Slow_Wakler
  • 2015年08月07日 20:22
  • 2278

Dijkstra算法详解

1.dijkstra算法简介 Dijkstra算法是由E.W.Dijkstra于1959年提出,又叫迪杰斯特拉算法,它应用了贪心算法模式,是目前公认的最好的求解最短路径的方法。算法解决的是有向图中单...
  • longshengguoji
  • longshengguoji
  • 2013年08月31日 19:03
  • 109889

Dijkstra算法和Floyd算法的实现

摘 要 在实际生活当中,我们需要知道从一个城市到达另一个城市的最短路径,在旅行时可以减少在路途中的时间。路由器的路由表的更新也需要知道两个路由之间的最短距离。很多的关于两点之间的最短路径问题都可...
  • zhuxiaodong030
  • zhuxiaodong030
  • 2013年07月05日 13:55
  • 2216

uva10806 - Dijkstra, Dijkstra. 最小费用流 求两条路权值之和最小

最小费用流是每次在残量图中求出起点到终点的最短路,然后再找出最短路上权值最小的一个边作为增广流量进行增广,更新流量,加上费用(最短路*增广流量)。找最短路用的不带链接表的spfa算法。因为每次增广后都...
  • corncsd
  • corncsd
  • 2014年01月21日 20:35
  • 485

UVa 10806 Dijkstra, Dijkstra (无向图+最小费用最大流+最短路:)

参考:http://www.cnblogs.com/scau20110726/archive/2012/12/09/2810654.html 题意:      固定起点1和终点n,从1到n,再从n回到...
  • Slow_Wakler
  • Slow_Wakler
  • 2015年08月07日 20:22
  • 2278

UVA 10806 — Dijkstra, Dijkstra. 费用流

原题:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=17...
  • L__emon
  • L__emon
  • 2015年05月26日 23:30
  • 468

UVA 10806 Dijkstra, Dijkstra. (费用流||最短路)

大意:从1到n然后再返回到1,每条路都只能走一次,最短路是多少。 思路1:可以用费用流建图,因为起始点和终点可以走两次所以源汇点流量为2.如果能返回的话肯定flow是>=2的,才能构成回路。...
  • Grit_ICPC
  • Grit_ICPC
  • 2016年05月26日 21:45
  • 219

UVa 10806 Dijkstra, Dijkstra. 费用流

Problem ? Dijkstra, Dijkstra. Time Limit: 10 seconds Dexter: "You don't understand. I c...
  • u013368721
  • u013368721
  • 2014年05月31日 14:13
  • 716

UVA 10806 Dijkstra, Dijkstra.(最小费用最大流)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem...
  • David_Jett
  • David_Jett
  • 2015年11月05日 22:20
  • 691

uva 10806 Dijkstra, Dijkstra.

uva 10806 Dijkstra, Dijkstra.
  • tengfei461807914
  • tengfei461807914
  • 2017年01月28日 12:37
  • 308
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 10806 - Dijkstra, Dijkstra.(费用流)
举报原因:
原因补充:

(最多只允许输入30个字)