Poj 2112 Optimal Milking【Floyd+二分+最大流Dinic】

Optimal Milking

Time Limit: 2000MS

 

Memory Limit: 30000K

Total Submissions: 16151

 

Accepted: 5788

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

Source

USACO 2003 U S Open

 

题目大意:有k个机器,有c头牛,每台机器最多给m个牛挤奶,每头牛都要选择一个机器去挤奶,问所有牛都有选择的情况下,最小的最远距离为多少。

输入 k,c,m

然后接下来k+c*k+c的一个邻接矩阵。


思路:


1、首先Floyd求一下两点间最短路。


2、然后对于这个最远距离我们二分求出。


3、然后针对这个二分当前值mid进行最大流判定:

①首先将k个机器连入汇点T,其权值为m,表示每台机器可以给m头牛挤奶。

②源点S,连接各个牛,其权值为1,表示这是一头牛。

③如果牛到机器的距离小于等于mid,那么建立一条从牛到机器的边,权值为1,表示这头牛可以到这个机器来挤奶。


4、然后建立好图之后,跑一遍Dinic,如果最大流==k,那么就说明当前情况是一个可行解,记录ans。二分结束之后,输出这个ans


Ac代码:

K,C输入反了,大家看的时候注意一下。


#iclude<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
int head[15500];
int div[15500];
int cur[15500];
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[1500000];
int map[500][500];
int c,k,m,n,ss,tt,cont;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void getmap(int mid)
{
    ss=n+1;
    tt=ss+1;
    cont=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=c;i++)
    {
        add(i,tt,m);
        add(tt,i,0);
    }
    for(int i=c+1;i<=n;i++)
    {
        add(ss,i,1);
        add(i,ss,0);
    }
    for(int i=c+1;i<=n;i++)
    {
        for(int j=1;j<=c;j++)
        {
            if(map[i][j]<=mid)
            {
                add(i,j,1);
                add(j,i,0);
            }
        }
    }
}
int makediv()
{
    memset(div,0,sizeof(div));
    queue<int >s;
    s.push(ss);
    div[ss]=1;
    while(!s.empty())
    {
        int u=s.front();
        if(u==tt)return 1;
        s.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(w&&div[v]==0)
            {
                div[v]=div[u]+1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    if(u==tt)return maxflow;
    int ret=0;
    for(int &i=cur[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(w&&div[v]==div[u]+1)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            e[i].w-=f;
            e[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
int Dinic(int mid)
{
    getmap(mid);
    int ans=0;
    while(makediv()==1)
    {
        memcpy(cur,head,sizeof(head));
        ans+=Dfs(ss,INF,tt);
    }
    if(ans==k)return 1;
    else return 0;
}
int main()
{
    while(~scanf("%d%d%d",&c,&k,&m))
    {
        for(int i=1;i<=c+k;i++)
        {
            for(int j=1;j<=c+k;j++)
            {
                scanf("%d",&map[i][j]);
                if(i!=j&&map[i][j]==0)
                map[i][j]=0x3f3f3f3f;
            }
        }
        n=c+k;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                for(int k=1;k<=n;k++)
                {
                    map[j][k]=min(map[j][i]+map[i][k],map[j][k]);
                }
            }
        }
        int ans,mid;
        int l=0;
        int r=10000;
        while(r-l>=0)
        {
            mid=(l+r)/2;
            if(Dinic(mid)==1)
            {
                r=mid-1;
                ans=mid;
            }
            else l=mid+1;
        }
        printf("%d\n",ans);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值