题目描述:
f个地区。已知各个地区之间的行走时间。
每个地区i有两个属性:
这个地区当前牛的个数,下雨的时候这个地区实际能够容纳牛的个数。
问至少需要多少时间,使所有的牛在下雨的时候都能够被容纳。
解题报告:
最大流,二分,拆点。
f个地区,每个地区i拆成i和i + f
源点s连接1~f,容量是第一个属性。
f+1~2f 连接t,容量是第二个属性。
二分至少需要的时间mid。
如果a地区到b地区的时间小于等于mid。
连接a到b+f。容量无穷大。
如果最大流等于牛的总数,mid值合法。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define Max 0x1fffffff
#define size 500
struct edge{int from, to, val, next;}e[140000];
int v[size], que[size], dis[size], cnt, cur[size];
void insert(int from, int to, int va)
{
e[cnt].from
= from, e[cnt].to = to; e[cnt].val = va;
e[cnt].next
= v[from];v[from] = cnt++;
e[cnt].from
= to, e[cnt].to = from; e[cnt].val = 0;
e[cnt].next
= v[to];v[to] = cnt++;
}
bool bfs(int n, int s, int t)
{
int head,
tail, id;
head = tail
= 0; que[tail++] = s;
memset(dis,
-1, sizeof(int) * n);dis[s] = 0;
while(head
< tail) // bfs,得到顶点i的距s的最短距离dis[i]
for(id = v[que[head++]]; id != -1; id = e[id].next)
if (e[id].val > 0 &&
dis[e[id].to] == -1)
{
dis[e[id].to] = dis[e[id].from] + 1;
que[tail++] = e[id].to;
if (e[id].to == t) return true;
}
return
false;
}
int Dinic(int n, int s, int t)
{
int maxflow
= 0, tmp, i;
while(bfs(n,
s, t))
{
int u = s, tail = 0;
for(i = 0; i < n; i++) cur[i] = v[i];
while(cur[s] != -1)
if (u != t && cur[u] != -1
&& e[cur[u]].val > 0
&& dis[u] != -1
&& dis[u] + 1 ==
dis[e[cur[u]].to])
{que[tail++] = cur[u]; u = e[cur[u]].to;}
else if (u == t)
{
for(tmp = Max, i = tail - 1; i >= 0; i--) tmp =
min(tmp, e[que[i]].val);
for(maxflow += tmp, i = tail - 1; i >= 0; i--)
{
e[que[i]].val -= tmp;
e[que[i] ^ 1].val += tmp;
if (e[que[i]].val == 0) tail = i;
}
u = e[que[tail]].from;
}
else
{