poj 2112 二分答案+最大流

/*
题意:k个挤奶机,每天可以给m头奶牛挤奶,让奶牛到达挤奶机的最大距离最小
题解:很明朗的二分答案的题目,不解释
先floyd求任意点之间的最短路,然后二分答案建图判断是否满足要求
*/
#include <cstdio>
#include <iostream>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<cstring>
using namespace std;
const int inf=1<<30;
const int maxn=1009;
int dis[maxn][maxn];
struct edge  
{   
    int v, next;   
    int val;   
} net[ 500100 ];   
int k,c,m;
int level[maxn], Qu[maxn], out[maxn],next[maxn];  
class Dinic {   
public:   
    int end;  
    Dinic() {   
        end = 0;   
        memset( next, -1, sizeof(next) );   
    }   
    inline void insert( int x, int y, int c) {   
        net[end].v = y, net[end].val = c,  
        net[end].next = next[x],   
        next[x] = end ++;   
        net[end].v = x, net[end].val = 0,  
        net[end].next = next[y],   
        next[y] = end ++;   
    }   
    bool BFS( int S, int E ) {   
        memset( level, -1, sizeof(level) );   
        int low = 0, high = 1;   
        Qu[0] = S, level[S] = 0;   
        for( ; low < high; ) {   
            int x = Qu[low];   
            for( int i = next[x]; i != -1; i = net[i].next ) {   
                if( net[i].val == 0 ) continue;   
                int y = net[i].v;   
                if( level[y] == -1 ) {   
                    level[y] = level[x] + 1;   
                    Qu[ high ++] = y;   
                }   
            }   
            low ++;   
        }   
        return level[E] != -1;   
    }    
    
    int MaxFlow( int S, int E ){   
        int maxflow = 0;   
        for( ; BFS(S, E) ; ) {   
            memcpy( out, next, sizeof(out) );   
            int now = -1;   
            for( ;; ) {   
                if( now < 0 ) {   
                    int cur = out[S];   
                    for(; cur != -1 ; cur = net[cur].next )    
                        if( net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1 )   
                            break;   
                    if( cur >= 0 ) Qu[ ++now ] = cur, out[S] = net[cur].next;   
                    else break;   
                }   
                int u = net[ Qu[now] ].v;   
                if( u == E ) {   
                    int flow = inf;   
                    int index = -1;   
                    for( int i = 0; i <= now; i ++ ) {   
                        if( flow > net[ Qu[i] ].val )   
                            flow = net[ Qu[i] ].val, index = i;   
                    }   
                    maxflow += flow;   
                    for( int i = 0; i <= now; i ++ )   
                        net[Qu[i]].val -= flow, net[Qu[i]^1].val += flow;   
                    for( int i = 0; i <= now; i ++ ) {   
                        if( net[ Qu[i] ].val == 0 ) {   
                            now = index - 1;   
                            break;   
                        }   
                    }   
                }   
                else{   
                    int cur = out[u];   
                    for(; cur != -1; cur = net[cur].next )    
                        if (net[cur].val && out[net[cur].v] != -1 && level[u] + 1 == level[net[cur].v])   
                            break;   
                    if( cur != -1 )   
                        Qu[++ now] = cur, out[u] = net[cur].next;   
                    else out[u] = -1, now --;   
                }   
            }   
        }   
        return maxflow;   
    }   
};   

void floyd()
{
	for(int g=1;g<=k+c;g++)
		{
			for(int i=1;i<=k+c;i++)if(g!=i&&dis[i][g]!=-1)
			{
				for(int j=1;j<=k+c;j++)if(g!=j&&dis[g][j]!=-1)
				{
					if(dis[i][j]==-1||dis[i][j]>dis[i][g]+dis[g][j])
					dis[i][j]=dis[i][g]+dis[g][j];
				}
			}
		}
}
void build(int lim,Dinic &my)
{
	for(int i=1;i<=k;i++)
	my.insert(0,i,m);
	
	for(int i=1;i<=k;i++)
	{
			for(int j=k+1;j<=k+c;j++)
			if(dis[i][j]<=lim&&dis[i][j]!=-1)
			{
			my.insert(i,j,1);
			}
	}
	for(int j=k+1;j<=k+c;j++)
	my.insert(j,k+c+1,1);
}
void solve()
{
	int low=0,hei=20000,mid;
	while(low<hei-1)
	{
		mid=(low+hei)/2;
		Dinic my;
		build(mid,my);
		if(my.MaxFlow(0,k+c+1)==c)
		hei=mid;
		else 
		low=mid;
	}
	printf("%d\n",hei);
}
int main()
{
	
	while(scanf("%d%d%d",&k,&c,&m)!=EOF)
	{
		for(int i=1;i<=k+c;i++)
		for(int j=1;j<=k+c;j++)
		{
		scanf("%d",&dis[i][j]);
		if(i!=j&&dis[i][j]==0)
		dis[i][j]=-1;
		}
		floyd();
		solve();
	}
}

你可能感兴趣的:(c,struct,Class,insert,Build)