Cod sursa(job #918211)

Utilizator apopeid13Apopeid Alejandro apopeid13 Data 18 martie 2013 18:16:03
Problema Cc Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<fstream>
#include<queue>
#define inf 1<<20
using namespace std;
 
int i,j,n,m,r,min1,c[210][210],x,cost[210][210];
int sursa,dest,rez=0,d[210],b[210],tata[210];
queue<int> q;
int det()
{
    int x,i;
    for(i=0;i<=dest;++i)
    d[i]=inf,b[i]=tata[i]=0;
    d[0]=0;
    q.push(0);
    while(!q.empty())
    {
       x=q.front();
       b[x]=0;
       for(i=0;i<=dest;++i)
       if(c[x][i]&&d[i]>d[x]+cost[x][i])
       {
           d[i]=d[x]+cost[x][i];
           tata[i]=x;
           if(!b[i])
           {
               b[i]=1;
               q.push(i);
           }
       }
       q.pop();
    }
    if(d[dest]!=inf)
    {
        for(i=dest;i!=sursa;i=tata[i])
        {
            --c[tata[i]][i];
            ++c[i][tata[i]];
        }
        return d[dest];
    }
    return 0;
}
int main()
{
    ifstream f("cc.in");
    ofstream g("cc.out");
    f>>n;
    sursa=0,dest=2*n+1;
    for(i=1;i<=n;++i)
    {
        c[sursa][i]=c[n+i][dest]=1;
        cost[sursa][i]=cost[n+i][dest]=0;
    for(j=1;j<=n;++j)
    {
        f>>cost[i][j+n];
        c[i][j+n]=1;
        cost[j+n][i]-=cost[i][j+n];
    }
    }
 
    r=1;
    while(r)
    {
        r=det();
        rez+=r;
    }
    g<<rez<<"\n";
    return 0;
}