【最大流+最短路+二分】POJ-2391 Ombrophobic Bovines

30 篇文章 0 订阅
20 篇文章 0 订阅
Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
   

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.
————————————————————害羞的分割线————————————————————
前言:唉!自己给模板加了个“优化”,结果还是错的。。。。。。耽误了好些时间。
思路:建图还是自己来咯~一开始直接以草地作为顶点,然后跑完最短路之后连接草地之间的边,结果发现,奶牛可以先从1走到2,再从2走到3,这样的话即使1和3之间没有边,也没意义了。
后来我发现既然跑完最短路,那么奶牛仅可能“最多走一次”,即
1. 呆在原地
2. 从i走到j(因为两点之间的点被Floyd省略了,之后就不能再走了)
所以将网络划分为两层:
  | 1 | 1 |
s| 2 | 2 |t
  | 3 | 3 |
这样一来每个点被拆成两个点。这就是拆点思想。(例如从第一层的1走到第二层的2,从第一层的2走到第二层的3,这样从第一层的1是走不到第二层的3的)
建图完成,其余就是Floyd+二分+Dinic。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const LL LNF = 2000000000000LL;
/****************************************/
const int N = 555, M = 222222;
int tot, head[N], cur[N], lev[N], q[N], s[N], S, T;
struct Node {
	int u, v, cap;
	int next;
}edge[M];
int n, m, cow, grass[N][2];
LL G[N][N];

void init()
{
	tot = 0; memset(head, -1, sizeof(head));
}

void add(int u, int v, int c)
{
	edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;
	edge[tot].next = head[u]; head[u] = tot++;
}

void build(LL maxi)
{
	for(int i = 1; i <= n; i++) {
		add(S, i, grass[i][0]); add(i, S, 0);
		for(int j = n+1; j <= (n<<1); j++) {
			if(G[i][j-n] <= maxi) {
				add(i, j, INF); add(j, i, 0);
			}
		}
	}
	for(int j = n+1; j <= (n<<1); j++) {
		add(j, T, grass[j-n][1]); add(T, j, 0);
	}
}

bool bfs()
{
	memset(lev, -1, sizeof(lev));
	lev[S] = 0;
	int fron = 0, rear = 0;
	q[rear++] = S;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(edge[i].cap && lev[v] == -1) {
				lev[v] = lev[u] + 1;
				q[rear%N] = v; rear++;
				if(v == T) return true;
			}
		}
	}
	return false;
}

bool Dinic(LL maxi)
{
	int ret = 0;
	init();
	build(maxi);
	while(bfs()) {
		memcpy(cur, head, sizeof(head));
		int u = S, top = 0;
		while(1) {
			if(u == T) {
				int mini = INF, loc;
				for(int i = 0; i < top; i++) {
					if(mini > edge[s[i]].cap) {
						mini = edge[s[i]].cap;
						loc = i;
					}
				}
				for(int i = 0; i < top; i++) {
					edge[s[i]].cap -= mini;
					edge[s[i]^1].cap += mini;
				}
				ret += mini;
				top = loc;
				u = edge[s[top]].u;
				if(!mini) lev[edge[s[top]].v] = -1;
			}
			int &i = cur[u];
			for(; i != -1; i = edge[i].next) {
				int v = edge[i].v;
				if(edge[i].cap && lev[v] == lev[u] + 1) break;
			}//找允许弧
			if(i != -1) {
				s[top++] = i;
				u = edge[i].v;
			}
			else {
				if(!top) break;
				lev[u] = -1;
				u = edge[s[--top]].u;
			}
		}
	}
	return ret == cow;
}

LL bin_S(LL maxi)
{
	LL low = 0, high = maxi + 1;
	while(low < high) {
		LL mid = (low + high) >> 1;
		if(Dinic(mid)) {
			high = mid;
		}
		else {
			low = mid + 1;
		}
	}
	return high;
}

void Floyd()
{
	for(int k = 1; k <= n; k++) {
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
			}
		}
	}
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	scanf("%d%d", &n, &m);
	cow = 0;
	S = 0; T = n << 1 | 1;
	for(int i = 1; i <= n; i++) {
		scanf("%d%d", &grass[i][0], &grass[i][1]);
		cow += grass[i][0];
	}
	int u, v, dis;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			G[i][j] = LNF;
			if(i == j) G[i][j] = 0;
		}
	}
	for(int i = 1; i <= m; i++) {
		scanf("%d%d%d", &u, &v, &dis);
		G[v][u] = G[u][v] = min(G[u][v], 1LL*dis);
	}
	Floyd();
	LL ans = bin_S(LNF);
	printf("%lld\n", ans >= LNF ? -1 : ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值