poj 2112 최대 흐름 + floyd + 2 점 답

4450 단어 cSAP
/*
    :
k   ,        m  。
c  ,     1      。
                。
 :             ,            ,     。
  :
            floyd          d[i][j]!
    :    s->         1,         1,   t     m
  ,max_flow==c          ans
          ans     Add  
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define M 41000
const int inf=1<<29;
int gap[M],dis[M],pre[M],cur[M];
int NE,NV,sink,source,d[402][402];
int head[M],k,c,m,ans;
struct Node
{
    int c,pos,next;
} E[999999];
#define FF(i,NV) for(int i=0;i<NV;i++)
int sap(int s,int t)
{
    //memset(pre,-1,sizeof(pre));
    memset(dis,0,sizeof(int)*(NV+1));
    memset(gap,0,sizeof(int)*(NV+1));
    FF(i,NV) cur[i] = head[i];
    int u = pre[s] = s,maxflow = 0,aug = 1<<29;
    gap[0] = NV;
    while(dis[s] < NV)
    {
loop:
        for(int &i = cur[u]; i != -1; i = E[i].next)
        {
            int v = E[i].pos;
            if(E[i].c && dis[u] == dis[v] + 1)
            {
                aug=min(aug,E[i].c);
                pre[v] = u;
                u = v;
                if(v == t)
                {
                    maxflow += aug;
                    for(u = pre[u]; v != s; v = u,u = pre[u])
                    {
                        E[cur[u]].c -= aug;
                        E[cur[u]^1].c += aug;
                    }
                    aug = 1<<29;
                }

                goto loop;
            }
        }

        int mindis = NV;
        for(int i = head[u]; i != -1 ; i = E[i].next)
        {
            int v = E[i].pos;
            if(E[i].c && mindis > dis[v])
            {
                cur[u] = i;
                mindis = dis[v];
            }
        }
        if( (--gap[dis[u]]) == 0)break;
        gap[ dis[u] = mindis+1 ] ++;
        u = pre[u];
    }
    return maxflow;
}
void addEdge(int u,int v,int c )
{
    E[NE].c = c;
    E[NE].pos = v;
    E[NE].next = head[u];
    head[u] = NE++;
    E[NE].c = 0;
    E[NE].pos = u;
    E[NE].next = head[v];
    head[v] = NE++;
}
void floyd(int n)
{
    int i,j,k;
    for(k=1; k<=n; k++)
    {
        for(i=1; i<=n; i++)
        {
            if(d[i][k]!=inf)
                for(j=1; j<=n; j++)
                {
                    if(d[k][j]!=inf&&i!=j)
                    {
                        d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
                        ans=max(d[i][j],ans);
                    }
                }
        }
    }
}
int ok(int mid)
{
    int i,j;
    NE=0;
    memset(head,-1,sizeof(int)*(NV+1));
    for(i=1;i<=k;i++) addEdge(i,sink,m);
    for(i=k+1;i<=k+c;i++) addEdge(source,i,1);
    for(i=k+1;i<=k+c;i++)
    {
        for(j=1;j<=k;j++)
        {
            if(d[i][j]<=mid) addEdge(i,j,1);
        }
    }
    if(sap(source,sink)==c) return 1;
    return 0;
}
int main()
{
    int i,j;
    while(scanf("%d%d%d",&k,&c,&m)!=EOF)
    {
        source=ans=0;
        sink=k+c+1;
        NV=sink+1;
        for(i=1; i<=k+c; i++)
        {
            for(j=1; j<=k+c; j++)
            {
                scanf("%d",&d[i][j]);
                if(d[i][j]==0) d[i][j]=inf;
            }
        }
        floyd(k+c);
        int l=1,r=ans;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(ok(mid)) r=mid-1;
            else l=mid+1;
        }
        printf("%d
",l); } return 0; } /* 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 */

좋은 웹페이지 즐겨찾기