POJ 2112 Optimal Milking 最大流 + 二分 + floyd

Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 14245 Accepted: 5132
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. 

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



呃。坑爹代码。。错了N次。。重新打了一变就好了。。还是不知道错在哪里了。。。

题目问奶牛要去挤奶场。一个挤奶场最多可以容纳n头奶牛  典型的最大流问题

先用floyd算出奶牛去挤奶场的最短路径

然后在二分枚举他如果所有的路径都低于mid的时候,最大流是否还是为C(也就是所有奶牛都能匹配到挤奶场)

用的kuangbin神的板 orz kuangbin

最后上代码

#include <iostream>
#include<stdio.h>  
#include<string.h>  

using namespace std;
const int MAXN = 100010 ; //点数最大值  
const int MAXM = 400010 ; //边数最大值  
const int INF = 0x3f3f3f3f;  

int K,C,M,V;
struct Edge{  
    int to,next,cap,flow;  
}edge[MAXM];//注意是MAXM  
int tol;  
int head[MAXN];  
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];  
int imap[333][333];
void floyd(){
	int i,j,k;
	for(k=0;k<V;k++){
		for(i=0;i<V;i++){
			for(j=0;j<V;j++){
				imap[i][j]=min(imap[i][j],imap[i][k]+imap[k][j]);
			}
		}
	}
	
	
/*
	printf("测试输出\n");
	for(i=0;i<V;i++){
		for(j=0;j<V;j++){
			printf("%d  ",imap[i][j]);
		}
		printf("\n");
	}*/
}

void init(){  
    tol = 0;  
    memset(head,-1,sizeof(head));  
}   

void addedge(int u,int v,int w,int rw=0){  
    edge[tol].to = v;  
    edge[tol].cap = w;  
    edge[tol].next = head[u];  
    edge[tol].flow = 0;  
    head[u] = tol++;  
    edge[tol].to = u;  
    edge[tol].cap = rw;  
    edge[tol].next = head[v];  
    edge[tol].flow = 0;  
    head[v] = tol++;  
}  
  //最大流开始 
int sap(int start,int end,int N){  
    memset(gap,0,sizeof(gap));  
    memset(dep,0,sizeof(dep));  
    memcpy(cur,head,sizeof(head));  
    int u = start;  
    pre[u] = -1;  
    gap[0] = N;  
    int ans = 0;  
    while(dep[start] < N){  
        if(u==end){  
            int Min = INF;  
            for(int i=pre[u];i!= -1; i=pre[edge[i^1].to])  
                if(Min > edge[i].cap - edge[i].flow)  
                    Min = edge[i].cap - edge[i].flow;  
                      
            for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]){  
                edge[i].flow += Min;  
                edge[i^1].flow -=Min;  
            }  
            u=start;  
            ans +=Min;  
            continue;  
        }  
        bool flag = false;  
        int v;  
        for(int i= cur[u];i!=-1;i=edge[i].next){  
            v=edge[i].to;  
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){  
                flag=true;  
                cur[u]=pre[v]=i;  
                break;  
            }  
        }  
        if(flag){  
            u=v;  
            continue;  
        }  
        int Min = N;  
        for(int i=head[u];i!= -1;i=edge[i].next)  
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min){  
                Min=dep[edge[i].to];  
                cur[u] = i;  
            }  
        gap[dep[u]]--;  
        if(!gap[dep[u]]) return ans;  
        dep[u] = Min +1;  
        gap[dep[u]]++;  
        if(u!=start) u = edge[pre[u]^1].to;  
    }  
    return ans;  
}  
//最大流结束 

bool solve(int mid){
	init();
	int i,j,k;
	int t=V+1,s=V;
	//超级汇点 
	for(i=0;i<K;i++){
		addedge(i,t,M);
	}
	for(i=K;i<V;i++){
		addedge(s,i,1);
	}
	for(i=0;i<K;i++){
		for(j=K;j<V;j++){
			if(imap[i][j]<=mid){
				addedge(j,i,1);
			}
		}
	}
	int ans=sap(s,t,t+1);
	return ans>=C;
}

int main(){  
    int m,n,q,p;  
    int i,j,k,a,b,c;  
    scanf("%d%d%d",&K,&C,&M);
    V=K+C;
	for(i=0;i<V;i++){
		for(j=0;j<V;j++){
			int tem;
			scanf("%d",&tem);
			if(tem!=0){
				imap[i][j]=tem;
			}else{
				imap[i][j]=INF;
			}
		}
	}
	floyd();
	int l=0,r=200*V;
	while(r-l>1){
		int mid=(r+l)>>1;
		bool ans=solve(mid);
		if(ans){
			r=mid;
		}else{
			l=mid;
		}
	}
	printf("%d\n",r);
    
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值