poj2112 网络流+二分答案
Posted Billyshuai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2112 网络流+二分答案相关的知识,希望对你有一定的参考价值。
Optimal Milking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 18083 | Accepted: 6460 | |
Case Time Limit: 1000MS |
Description
FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.
Each milking point can "process" at most M (1 <= M <= 15) cows each day.
Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.
Each milking point can "process" at most M (1 <= M <= 15) cows each day.
Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.
Input
* Line 1: A single line with three space-separated integers: K, C, and M.
* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.
* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.
Output
A single line with a single integer that is the minimum possible total distance for the furthest walking cow.
Sample Input
2 3 2 0 3 2 1 1 3 0 3 2 0 2 3 0 1 0 1 2 1 0 2 1 0 0 2 0
Sample Output
2
#include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=280; int head[N],dis[N],tot,K,C,M,st,ed,mp[N][N]; struct node { int to,next,w; } e[N*N*2]; void add(int u,int v,int w) { e[tot].to=v; e[tot].next=head[u]; e[tot].w=w; head[u]=tot++; } void init() { tot=0; memset(head,-1,sizeof(head)); } bool bfs() { queue<int>Q; memset(dis,-1,sizeof(dis)); Q.push(0); dis[0]=1; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=head[u]; i+1; i=e[i].next) { int v=e[i].to; if(dis[v]==-1&&e[i].w>0) { dis[v]=dis[u]+1; Q.push(v); if(v==K+C+1) return true; } } } return false; } int dfs(int s,int low) { if(s==ed||low==0) return low; int ans=low,a; for(int i=head[s]; i+1; i=e[i].next) { int v=e[i].to; if(e[i].w>0&&dis[v]==dis[s]+1&&(a=dfs(v,min(ans,e[i].w)))) { ans-=a; e[i].w-=a; e[i^1].w+=a; if(!ans) return low; } } if(ans==low) dis[s]=-1; return low-ans; } void build(int lim) { init(); for(int i=1; i<=K; ++i) for(int j=K+1; j<ed; ++j) if(mp[i][j]<=lim&&mp[i][j]) { add(i,j,1); add(j,i,0); } for(int i=1; i<=K; ++i) add(0,i,M),add(i,0,0); for(int i=K+1; i<ed; ++i) add(i,ed,1),add(ed,i,0); } bool Ju() { int ans=0; while(bfs()) ans+=dfs(0,99999999); if(ans==C) return true; return false; } void Floyd() { for(int k=1; k<ed; ++k) for(int i=1; i<ed; ++i) if(mp[i][k]) for(int j=1; j<ed; ++j) if(mp[k][j]) { if(mp[i][j]==0) mp[i][j]=mp[i][k]+mp[k][j]; else if(mp[i][j]>mp[i][k]+mp[k][j]) mp[i][j]=mp[i][k]+mp[k][j]; } } int main() { while(scanf("%d%d%d",&K,&C,&M)!=EOF) { st=0; ed=K+C+1; for(int i=1; i<ed; ++i) for(int j=1; j<ed; ++j) scanf("%d",&mp[i][j]); Floyd(); int r=20000000,l=0; while(r!=l) { int mid=(r+l)>>1; build(mid); if(Ju()) r=mid; else l=mid+1; } printf("%d\n",(l+r)>>1); } }
以上是关于poj2112 网络流+二分答案的主要内容,如果未能解决你的问题,请参考以下文章
POJ 2112 Optimal Milking(二分+最大流)
POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分