最大流例题 — poj_2391

/***********************************************************
 * poj2391 http://poj.org/problem?id=2391
Description
        FJ's cows really hate getting wet so much that the mere thought of getting caught 
    in the rain makes them shake in their hooves. They have decided to put a rain siren on
    the farm to let them know when rain is approaching. They intend to create a rain evacuation
    plan so that all the cows can get to shelter before the rain begins. Weather forecasting is
    not always correct, though. In order to minimize false alarms, they want to sound the siren
    as late as possible while still giving enough time for all the cows to get to some shelter.
    The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) 
    paths connects them. The paths are wide, so that any number of cows can traverse a path in 
    either direction.
        Some of the farm's fields have rain shelters under which the cows can shield themselves.
    These shelters are of limited size, so a single shelter might not be able to hold all the cows.
    Fields are small compared to the paths and require no time for cows to traverse.
        Compute the minimum amount of time before rain starts that the siren must be sounded so that
    every cow can get to some shelter.
Input
    Line 1: Two space-separated integers: F and P
    Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000)
    is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the 
    shelter in that field can hold. Line i+1 describes field i.
    Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers
    (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000)
    is how long any cow takes to traverse it.
Output
    Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan
    their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".
Sample Input
3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120
Sample Output
110
Hint
OUTPUT DETAILS:
In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field
1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field
under the shelter in field 3. Although there are other plans that will get all the cows under a shelter,
none will do it in fewer than 110 time units.
**********************************************************/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF = 2000000000;
const int maxV = 1000;
const int maxE = 200000;

int nnode, nedge, src, dest, total;
long long maxT, minT;
int adj[maxE], capa[maxE], flow[maxE], next[maxE], cpy[maxE];
int head[maxV], begin[maxV], q[maxV], dist[maxV], cpyhead[maxV];
long long d[maxV][maxV];

//初始化流网络
void init(int n, int s, int t) {
	nnode = n;
	src = s;
	dest = t;
	nedge = 0;
	memset(head, -1, nnode*sizeof(head[0]));
}

//将每条边拆分成两条有向边,邻接表表示,
//容量分别为c1, c2, 流量均为0
void addEdge(int u, int v, int c1, int c2) {
	adj[nedge] = v; capa[nedge] = c1; flow[nedge] = 0;
	next[nedge] = head[u]; head[u] = nedge++;
	adj[nedge] = u; capa[nedge] = c2; flow[nedge] = 0;
	next[nedge] = head[v]; head[v] = nedge++;
}

//用bfs建立层次图,并判断是否存在从源点到汇点的路径
bool bfs() {
	int front = 0, rear = 1;
	memset(dist, -1, nnode*sizeof(dist[0]));
	q[front] = src; dist[src] = 0;
 	while (front != rear) {		
		int u = q[front];
		for (int i = head[u]; i != -1; i = next[i]) {
			int v = adj[i];
			if (capa[i] > flow[i] && dist[v] == -1) {
				dist[v] = dist[u] + 1;
				q[rear++] = v;
			}			
		}
		front++;
	}
	return dist[dest] != -1;
}

//dfs寻找曾广路,利用引用的技巧,
//起到了一定的剪枝的效果,加速了后续的曾广
//用i和i^1表示了addEdge中每次添加的两条配对的边
int dfs(int u, int exp) {
	if (u == dest) return exp;
	for (int &i = begin[u]; i != -1; i = next[i]) {
		int md = 0, v = adj[i];
		if (capa[i] > flow[i] && dist[v] == dist[u]+1 &&
			(md = dfs(v, min(exp, capa[i]-flow[i]))) > 0) {
			flow[i] += md;
			flow[i^1] -= md;
			return md;
		}
	}
	return 0;
}

//dinic算法,用bfs在残量网络上建立层次图
//然后用dfs在层次图上寻找增广路,结合了
//bfs和dfs的优势
int dinic() {
	int result = 0;
	while (bfs()) {		
		memcpy(begin, head, nnode*sizeof(head[0]));
		while (true) {
			int delta = dfs(src, INF);
			if (delta == 0) break;
			result += delta;
		}
	}
	return result;
}

int F, P;
int num1[510], num2[510];
void readData() {	
	int u, v;
    long long T;

	total = 0;
	scanf("%d%d", &F, &P);	
	for (int i = 1; i <= F; ++i) {
		scanf("%d%d", &num1[i], &num2[i]);
		total += num1[i];
	}
	for (int i = 1; i <= F; ++i)
		for (int j = i+1; j <= F; ++j)
			d[i][j] = d[j][i] = (long long)INF*1000;
	maxT = 0;
	for (int i = 1; i <= P; ++i) {
		scanf("%d%d%I64d", &u, &v, &T);
		if (T < d[u][v]) d[u][v] = d[v][u] = T;
		maxT += T;
	}
} 

//在二分查找中进行检测时,每次建立一个流网络
//用dinic算法求最大流
bool check(long long mid) {
	init(2*F+2, 0, 2*F+1);
	for (int i = 1; i <= F; ++i) {
		addEdge(src, i, num1[i], 0);
		addEdge(i, F+i, INF, 0);
		addEdge(F+i, dest, num2[i], 0);
		for (int j = i+1; j <= F; ++j)
			if (d[i][j] <= mid) {
				addEdge(i, F+j, INF, 0);
				addEdge(j, F+i, INF, 0);
			}
	}	
	
	return dinic() == total;
}

void solve() {
	for (int k = 1; k <= F; ++k)
		for (int i = 1; i <= F; ++i) if (i != k)
			for (int j = 1; j <= F; ++j) if (j != i && j != k)
				d[i][j] = min(d[i][j], d[i][k]+d[k][j]);	
	long long ans = -1;
	long long start = 0, end = maxT+1;
	while (start < end) {		
		long long mid = start + (end-start)/2;
		if (check(mid))
			ans = end = mid;
		else
			start = mid + 1;
	}
	printf("%I64d\n", ans);	
}

int main() {
	readData();
	solve();

	return 0;
}

 

posted @ 2013-12-22 16:33  wlqsun  阅读(245)  评论(0编辑  收藏  举报