poj2112 二分+最大流

來源:互聯網
上載者:User
Optimal MilkingTime Limit : 4000/2000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other)Total Submission(s) : 1   Accepted Submission(s) : 1Problem DescriptionFJ 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.  


OutputA single line with a single integer that is the minimum possible total distance for the furthest walking cow.  


Sample Input

2 3 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0
 


Sample Output

2
 


SourcePKU 和上一題基本一個思路額~~本來想著能不能直接建邊不用floyd 後來覺得不行啊~~

#include <cstring>#include <cstdio>#include <queue>#define MAXN 5000#define MAXM 500000#define inf 0x3f3f3f3fusing namespace std;struct node{    int u,v,f,c;};node edge[MAXM*3];int first[MAXN],next[MAXM*3];int gap[MAXN],d[MAXN],curedge[MAXN],pre[MAXN];int cc;inline void add_edge(int u,int v,int f,int c){    edge[cc].u=u;    edge[cc].v=v;    edge[cc].f=f;    edge[cc].c=c;    next[cc]=first[u];    first[u]=cc;    cc++;    edge[cc].u=v;    edge[cc].v=u;    edge[cc].f=0;    edge[cc].c=0;    next[cc]=first[v];    first[v]=cc;    cc++;}int ISAP(int s,int t,int n){    int cur_flow,flow_ans=0,u,tmp,neck,i,v;    memset(d,0,sizeof(d));    memset(gap,0,sizeof(gap));    memset(pre,-1,sizeof(pre));    for(i=0;i<=n;i++)        curedge[i]=first[i];    gap[0]=n+1;    u=s;    while(d[s]<=n)    {        if(u==t)        {            cur_flow=inf;            for(i=s;i!=t;i=edge[curedge[i]].v)            {                if(cur_flow>edge[curedge[i]].f)                {                    neck=i;                    cur_flow=edge[curedge[i]].f;                }            }            for(i=s;i!=t;i=edge[curedge[i]].v)            {                tmp=curedge[i];                edge[tmp].f-=cur_flow;                edge[tmp^1].f+=cur_flow;            }            flow_ans+=cur_flow;            u=neck;        }        for(i=curedge[u];i!=-1;i=next[i])        {            v=edge[i].v;            if(edge[i].f&&d[u]==d[v]+1)                break;        }        if(i!=-1)        {            curedge[u]=i;            pre[v]=u;            u=v;        }        else        {            if(0==--gap[d[u]])                break;            curedge[u]=first[u];            for(tmp=n+5,i=first[u];i!=-1;i=next[i])                if(edge[i].f)                    tmp=min(tmp,d[edge[i].v]);            d[u]=tmp+1;            ++gap[d[u]];            if(u!=s)                u=pre[u];        }    }    return flow_ans;}void build(int limit,int cnt,int m){    int i;    for(i=0;i<cnt;i=i+2)    {        edge[i].f=m;        edge[i^1].f=0;    }    for(i=cnt;i<cc;i=i+2)    {        edge[i].f=(edge[i].c<=limit);        edge[i^1].f=0;    }}int dist[300][300];void Floyd(int n){    int i,j,k;    for(k=1;k<=n;k++)    {        for(i=1;i<=n;i++)        {            for(j=1;j<=n;j++)            {                if(dist[i][k]!=inf&&dist[k][j]!=inf&&                   dist[i][j]>dist[i][k]+dist[k][j])                {                    dist[i][j]=dist[i][k]+dist[k][j];                }            }        }    }}int main(){    int k,c,m;    while(scanf("%d%d%d",&k,&c,&m)!=EOF)    {        memset(first,-1,sizeof(first));        memset(next,-1,sizeof(next));        cc=0;        int i,j;        int s=0,t=k+c+1;        for(i=1;i<=k;i++)            add_edge(i,t,m,0);        int cnt1=cc;        for(i=1;i<=k+c;i++)        {            for(j=1;j<=k+c;j++)            {                scanf("%d",&dist[i][j]);                if(dist[i][j]==0)                    dist[i][j]=inf;            }        }        Floyd(k+c);        for(i=k+1;i<=k+c;i++)        {            for(j=1;j<=k;j++)                add_edge(i,j,inf,dist[i][j]);        }        //int cnt2=cc;        for(i=k+1;i<=k+c;i++)            add_edge(s,i,1,0);        int l=0,r=400000;        int mid,ans=inf;        while(l<=r)        {            mid=(l+r)>>1;            build(mid,cnt1,m);            int res=ISAP(s,t,t);            if(res<c)                l=mid+1;            else if(res>=c)            {                ans=min(ans,mid);                r=mid-1;            }        }        printf("%d\n",ans);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.