您的位置:首页 > 其它

poj 2112(二分+最大流)

2011-09-01 20:42 423 查看
Optimal Milking

Time Limit: 2000MSMemory Limit: 30000K
Total Submissions: 6483Accepted: 2428
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

题目:http://poj.org/problem?id=2112

分析:这题一开始不知从何下手,后来发现只要把所有点之间的最短路径都做成一条容量为1边就行,然后增加源和汇,源与所有机器连上容量为m的边,牛与汇连上容量为1的边,然后二分答案,大于答案的边都删掉,判断是否可行即可。。。

代码:

#include<cstdio>
using namespace std;
const int mm=55555;
const int mn=333;
const int oo=1000000000;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
int d[mn][mn];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int main()
{
    int u,v,w,n,c,m,s,l,r,mid;
    while(scanf("%d%d%d",&n,&c,&m)!=-1)
    {
        s=n+c;
        for(u=1;u<=s;++u)
            for(v=1;v<=s;++v)
                scanf("%d",&d[u][v]);
        for(w=1;w<=s;++w)
            for(u=1;u<=s;++u)
                if(w!=u)for(v=1;v<=s;++v)
                    if(u!=v&&w!=v&&d[u][w]&&d[w][v])
                    {
                        if(d[u][v])d[u][v]=min(d[u][v],d[u][w]+d[w][v]);
                        else d[u][v]=d[u][w]+d[w][v];
                    }
        l=oo,r=0;
        for(u=1;u<=s;++u)
            for(v=1;v<=s;++v)
            if(d[u][v])
            {
                if(d[u][v]<l)l=d[u][v];
                if(d[u][v]>r)r=d[u][v];
            }
        while(l<r)
        {
            mid=(l+r)>>1;
            prepare(s+2,0,s+1);
            for(u=1;u<=n;++u)addedge(src,u,m);
            for(u=n+1;u<=s;++u)addedge(u,dest,1);
            for(u=1;u<=n;++u)
                for(v=n+1;v<=s;++v)
                    if(d[u][v]&&d[u][v]<=mid)addedge(u,v,1);
            if(Dinic_flow()==c)r=mid;
            else l=mid+1;
        }
        printf("%d\n",r);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: