Optimal Milking Time Limit : 4000/2000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 5 Accepted Submission(s) : 2 Problem 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 題意:奶牛要走到產奶機器,每頭牛只能到一個產奶機,每個機器有一定的容量。問走的最遠的牛所走的最短路徑是多少。 思路:floyd求最短路,二分答案,網絡流求解。先求出點直接的距離。選擇出路徑的上界作為二分的上界,(開始的時候二人上界搞錯了,wa了幾次)。 建邊:源點與牛建邊,容量為1,機器與匯點建邊,容量為機器的容納量。牛與機器之間,距離小於二分的答案的兩點建一條邊。sap判斷是否符合,符合改變上下界大小,最後輸出結果即可。每次二分的一個答案重新建邊時要重新初始化NE與head函數。開始的時候忘了,o(╯□╰)o…… [cpp] #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #define max(a,b) ((a)>(b)?(a):(b)) using namespace std; const int N=420; const int M=82000; const int INF=99999999; int n; int gap[N],dis[N],pre[N],head[N],cur[N]; int map[N][N]; int NE,NV; struct Node { int pos,next; int c; } E[M]; #define FF(i,NV) for(int i=0;i<NV;i++) int sap(int s,int t) { memset(dis,0,sizeof(int)*(NV+1)); memset(gap,0,sizeof(int)*(NV+1)); FF(i,NV) cur[i] = head[i]; int u = pre[s] = s,maxflow = 0; int aug =INF; gap[0] = NV; while(dis[s] < NV) { loop: for(int &i = cur[u]; i != -1; i = E[i].next) { int v = E[i].pos; if(E[i].c && dis[u] == dis[v] + 1) { aug=min(aug,E[i].c); pre[v] = u; u = v; if(v == t) { maxflow += aug; for(u = pre[u]; v != s; v = u,u = pre[u]) { E[cur[u]].c -= aug; E[cur[u]^1].c += aug; } aug =INF; } goto loop; } } if( (--gap[dis[u]]) == 0) break; int mindis = NV; for(int i = head[u]; i != -1 ; i = E[i].next) { int v = E[i].pos; if(E[i].c && mindis > dis[v]) { cur[u] = i; mindis = dis[v]; } } gap[ dis[u] = mindis+1 ] ++; u = pre[u]; } return maxflow; } void addEdge(int u,int v,int c ) { E[NE].c = c; E[NE].pos = v; E[NE].next = head[u]; head[u] = NE++; E[NE].c = 0; E[NE].pos = u; E[NE].next = head[v]; head[v] = NE++; } int ans; void floyed() { for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) { if(map[i][k]!=INF) { for(int j=1; j<=n; j++) { if(map[k][j]!=INF&&map[i][j]>map[i][k]+map[k][j]) { map[i][j]=map[i][k]+map[k][j]; ans=max(map[i][j],ans); } } } } } int main() { int k,c,m; while(cin>>k>>c>>m) { ans=-1; n=k+c; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { cin>>map[i][j]; //ans=max(ans,map[i][j]); if(map[i][j]==0) map[i][j]=INF; } floyed(); int low=0,high=ans,mid; //high的值應該最小為所有點對之間路徑中最長的,不是初始時中的最大值, //由於這個原因wa了幾次。…… while(low<=high) { NE=0; NV=k+c+2; memset(head,-1,sizeof(head)); mid=(low+high)/2; for(int i=1;i<=k;i++) addEdge(i,k+c+1,m); for(int i=k+1;i<=k+c;i++) addEdge(0,i,1); for(int i=k+1;i<=k+c;i++)//牛與機器建邊 for(int j=1;j<=k;j++) { if(map[i][j]<=mid) addEdge(i,j,1); } if(sap(0,k+c+1)==c)//mid符合條件,則可以減小。 high=mid-1; else low=mid+1; } cout<<low<<endl; } return 0; } /* 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 */