Cod sursa(job #1024652)

Utilizator andrettiAndretti Naiden andretti Data 8 noiembrie 2013 22:09:33
Problema Cc Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<queue>
#define maxn 205
#define inf 0x3f3f3f3f
using namespace std;

int n,S,D,flow;
int d[maxn],used[maxn],father[maxn];
int c[maxn][maxn],Cost[maxn][maxn];

void read()
{
    scanf("%d",&n); D=2*n+1;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
     {
         scanf("%d",&Cost[i][j+n]);
         Cost[j+n][i]=-Cost[i][j+n];
         c[i][j+n]=1;
     }
    for(int i=1;i<=n;i++)
     c[S][i]=c[i+n][D]=1;
}

int bellman()
{
    queue<int> q;
    int k;

    for(int i=1;i<=D;i++) d[i]=inf;
    memset(used,0,sizeof(used));
    for(q.push(S),used[S]=1;!q.empty();q.pop(),used[k]=0)
    {
        k=q.front();
        for(int i=1;i<=D;i++)
         if(c[k][i] && d[k]+Cost[k][i]<d[i])
         {
             d[i]=d[k]+Cost[k][i];
             father[i]=k;
             if(!used[i])
             {
                 q.push(i);
                 used[i]=1;
             }
         }
    }
    if(d[D]==inf) return 0;
    return 1;
}

void solve()
{
    while(bellman())
    {
        for(int i=D;i!=S;i=father[i])
          c[father[i]][i]--,c[i][father[i]]++;
        flow+=d[D];
    }
}

int main()
{
    freopen("cc.in","r",stdin);
    freopen("cc.out","w",stdout);

    read();
    solve();
    printf("%d",flow);

    fclose(stdin);
    fclose(stdout);
    return 0;
}