poj 2112 Optimal Milking(floyd+二分+最大流)

Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 15753 Accepted: 5617
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
题意:f台取奶机,c只奶牛,每台取奶机可以同时给m只奶牛取奶

取奶机与奶牛之间两两有一个距离,走过去需要时间,问你让所有奶牛取到奶,最少要多久?

思路:用floyd跑一遍求出两点间的最短距离,然后上界是两点间最大距离+1(最好初始权值也要比一遍),然后二分,每次把小于mid的边加进来,缩小范围,最后的mid就是最短距离。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 550
#define INF 1<<28
struct Edge
{
    int to,next,cap;
} edge[N*N];
int cnt,head[N],d[N];
int now[N],can[N];
int maxn,dp[N][N];
void addedge(int from,int to,int cap)
{
    edge[cnt].to=to;
    edge[cnt].cap=cap;
    edge[cnt].next=head[from];
    head[from]=cnt++;

    edge[cnt].to=from;
    edge[cnt].cap=0;
    edge[cnt].next=head[to];
    head[to]=cnt++;
}

int bfs(int s,int t)
{
    memset(d,-1,sizeof(d));
    queue<int> q;
    q.push(s);
    d[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(d[v]==-1&&edge[i].cap>0)
            {
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||f==0) return f;
    int flow=0;
    for(int i=head[s]; i!=-1&&flow<f; i=edge[i].next)
    {
        int v=edge[i].to;
        if(d[v]==d[s]+1&&edge[i].cap>0)
        {
            int x=min(f-flow,edge[i].cap);
            x=dfs(v,t,x);
            flow+=x;
            edge[i].cap-=x;
            edge[i^1].cap+=x;
            if(f==0) break;
        }
    }
    if(!flow) d[s]=-2;
    return flow;
}
int Dinic(int s,int t)///起点s终点t
{
    int flow=0,f;
    while(bfs(s,t))
    {
        while(f=dfs(s,t,INF))
            flow+=f;
    }
    return flow;
}
void floyd(int n)
{
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                if(dp[i][k]==0||dp[k][j]==0) continue;
                if(dp[i][j]==0||dp[i][j]>dp[i][k]+dp[k][j])
                {
                    dp[i][j]=dp[i][k]+dp[k][j];
                    maxn=max(maxn,dp[i][j]);
                }
            }
}
int main()
{
    int k,c,m,t;
    while(~scanf("%d %d %d",&k,&c,&m))
    {
        int S=0,T=k+c+2;
        int sum=c;
        maxn=-INF;
        for(int i=1; i<=k+c; i++)
            for(int j=1; j<=k+c; j++)
            {
                scanf("%d",&dp[i][j]);
                maxn=max(maxn,dp[i][j]);
            }
        floyd(k+c);
        int low=0,high=maxn+1,mid,ans=-1;
        while(low<high)
        {
            mid=(low+high)/2;
            cnt=0;
            memset(head,-1,sizeof(head));
            for(int i=1; i<=k; i++)
                addedge(i,T,m);
            for(int i=k+1; i<=k+c; i++)
                addedge(S,i,1);
            for(int i=k+1; i<=k+c; i++)
                for(int j=1; j<=k; j++)
                    if(dp[i][j]!=0&&dp[i][j]<=mid)
                        addedge(i,j,INF);
            if(Dinic(S,T)==sum) ans=high=mid;
            else low=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值