Description
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
* 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
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
题意:给定一个无向图,点 i 处有 Ai 头牛,点 i 处的牛棚能容纳 Bi 头牛,求一个最短时
间 T 使得在 T 时间内所有的牛都能进到某一牛棚里去。 (1 <= N <= 200, 1 <= M <=
1500, 0 <= Ai <= 1000, 0 <= Bi <= 1000, 1 <= Dij <= 1,000,000,000),此题题目告诉了所有的牛都是同时跑的,要求的就是最慢找到牛棚的那头牛用时最少是多少。
分析:由于n只有200,那么我们就可以用floyd预处理每两个点的最少用时(就是最短路)存在数组dis[i][j]里面。由于每个点有两个限制条件:牛的头数以及牛棚能容纳牛的头数,显然就是拆为两个点x和y,x点连接源点,容量为牛的头数,y点连接汇点,容量为能容纳牛的头数。接下来就是其他点之间的连接问题,因为此题要求的是用时最大的值最小,直接建图不行,看到最大值最小,我们很容易想到二分答案,对于每个二分的答案mid,我们把dis[i][j]<=mid的两个点互相连一条边,容量为inf,然后跑最大流,如果最大流等于所有牛的头数,那么这个答案ok,单调性很明显:如果最大流跑出来小于牛的头数,意思就是你给的时间mid太少了,人家还没跑完,所以增大时间,要是最大流跑出来等于了牛的头数,虽然这个答案可行,但我们要的是最优的,所以继续减少限制的时间看看行不行就可以了。注意这个题每次二分的mid并不代表mid就可以用来更新答案ans,因为建图的时候条件是dis[i][j]<=mid,那么就有可能所有的dis都小于mid,那么更新答案时就是所有小于mid中的最大的那个值。
dinic模板为大白书上的模板:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#define maxn 2000
#define LL long long
#define inf 0x7fffffff
#define INF 1e18
using namespace std;
struct edge
{
int from,to,cap,flow;
};
vector<edge> edges;
vector<int> G[maxn];
int d[maxn],cur[maxn];
bool vis[maxn];
int s,t;
void init()
{
for(int i=0;i<maxn;i++) G[i].clear();
edges.clear();
}
void add(int from,int to,int cap)
{
edges.push_back((edge){from,to,cap,0});
edges.push_back((edge){to,from,0,0});
int m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bfs()
{
memset(vis,false,sizeof(vis));
memset(d,0,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0,vis[s]=true;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0;i<G[x].size();i++)
{
edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==0) return a;
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++)
{
edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxflow()
{
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,inf);
}
return flow;
}
int cnt[300],cap[300];
LL dis[300][300],ans;
int F,P;
void build(LL mid)
{
ans=0;
for(int i=1;i<=F;i++)
{
add(s,i,cnt[i]);
add(i+F,t,cap[i]);
}
for(int i=1;i<=F;i++)
{
for(int j=1;j<=F;j++)
{
if(dis[i][j]<=mid)
{
ans=max(ans,dis[i][j]);
add(i,j+F,inf);
}
}
}
}
int main()
{
while(scanf("%d%d",&F,&P)==2)
{
s=0,t=2*F+1;
int sum=0;
for(int i=1;i<=F;i++)
{
scanf("%d%d",&cnt[i],&cap[i]);
sum+=cnt[i];
}
for(int i=1;i<=F;i++)
for(int j=1;j<=F;j++)
dis[i][j]=INF;
for(int i=1;i<=F;i++) dis[i][i]=0;
for(int i=1;i<=P;i++)
{
int x,y;
LL v;
scanf("%d%d%lld",&x,&y,&v);
if(v<dis[x][y]) dis[x][y]=dis[y][x]=v;
}
LL L=INF,R=0,res=INF;
for(int k=1;k<=F;k++)
for(int i=1;i<=F;i++)
for(int j=1;j<=F;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
for(int i=1;i<=F;i++)
for(int j=1;j<=F;j++)
{
L=min(L,dis[i][j]);
R=max(R,dis[i][j]);
}
while(L<=R)
{
LL mid;
mid=(L+R)/2;
init();
build(mid);
int tem=maxflow();
if(tem!=sum) L=mid+1;
else if(tem==sum)
{
res=min(res,ans);
R=mid-1;
}
}
if(res<INF) printf("%lld\n",res);
else puts("-1");
}
return 0;
}