poj 2112(网络流)
通过二分边,来求得最小的最大路径。。。
更原始的思路是,从1到最大边的长度, 每次加入这个权值的边,然后用网络流算法求出是否流过的流等于总的奶牛数.
当时注意这题二分的上界不是200, 题中只是说给的边是小于200的,但是奶牛到挤奶器的距离可能远大于200.
Optimal Milking
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. 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. 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 Source |
#include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <iostream> using namespace std; #define N 250 #define M 2001000 #define INF 0x3fffff struct node { int u,v; int w; }edge1[100100]; struct node1 { int to,w,next; }edge[M]; int pre[10*N]; int g[N][N]; vector<int> weight[20100]; int gap[10*N],lv[10*N]; int k,c,m; int cnt1,cnt; int n,nn; int s,t; int ans; int sum; void add_edge(int u,int v,int w) { edge[cnt].to=v; edge[cnt].w=w; edge[cnt].next=pre[u]; pre[u]=cnt++; } int gdfs(int k,int w) { if(k==t) return w; int f=0; int mi=nn-1; for(int p=pre[k];p!=-1;p=edge[p].next) { int v=edge[p].to,tw=edge[p].w; if(tw!=0) { if(lv[k]==lv[v]+1) { int tmp=gdfs(v,min(tw,w-f)); f+=tmp; edge[p].w-=tmp; edge[p^1].w+=tmp; if(f==w||lv[s]==nn) break; } if(lv[v]<mi) mi=lv[v]; } } if(f==0) { gap[lv[k]]--; if( gap[ lv[k] ]==0 ) { lv[s]=nn; } lv[k]=mi+1; gap[lv[k]]++; } return f; } int sap() { memset(lv,0,sizeof(lv)); memset(gap,0,sizeof(gap)); gap[0]=nn; while(lv[s]<nn) { sum+=gdfs(s,INF); } return sum; } int check(int mid) { sum=0; memset(pre,-1,sizeof(pre)); cnt=0; for(int i=1;i<=c;i++) { add_edge(s,i,1); add_edge(i,s,0); } for(int i=1;i<=k;i++) { add_edge(c+2*i-1,c+2*i,m); add_edge(c+2*i,c+2*i-1,0); add_edge(c+2*i,t,m); add_edge(t,c+2*i,0); } for(int i=1;i<=mid;i++) { if(weight[i].size()==0) continue; for(int j=0;j<weight[i].size();j++) { int x,y; x=edge1[weight[i][j]].u; y=edge1[weight[i][j]].v; x=x-k; y=c+2*y-1; add_edge(x,y,1); add_edge(y,x,0); } } if(sap()==ans) { return 1; } else return 0; } int main() { scanf("%d%d%d",&k,&c,&m); ans=c; n=k+c; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { scanf("%d",&g[i][j]); if(g[i][j]==0&&i!=j) { g[i][j]=INF; } } ///// for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(g[i][j]>g[i][k]+g[k][j]) { g[i][j]=g[i][k]+g[k][j]; } ///// 下面构图 for(int i=1;i<=k;i++) for(int j=k+1;j<=n;j++) { if( g[i][j] < INF ) { edge1[cnt1].u=j; edge1[cnt1].v=i; edge1[cnt1].w=g[i][j]; weight[ g[i][j] ].push_back(cnt1); cnt1++; } } s=0; t=c+2*k+1; nn=t+1; int b,d; b=1; d=20100; while(b<d) { int mid=(b+d)/2; if(check(mid)==1) { d=mid; } else { b=mid+1; } } printf("%d",b); return 0; }