Cod sursa(job #906138)

Utilizator dicu_dariaDaria Dicu dicu_daria Data 6 martie 2013 15:39:33
Problema Cc Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>
#include <queue>
#define N 110
#define oo int(2e9)
using namespace std;

queue <int> Q;
bool inq[N];
int sol, i, j, n, x, S, D;
int prev[N], Cost[N][N], Dist[N], C[N][N];

bool Bellman()
{
    for(i = 0; i <= 2*n + 1; i++) Dist[i] = oo;
    Dist[S] = 0;
    Q.push(S);
    while(!Q.empty())
    {
        x = Q.front();
        Q.pop();
        inq[x] = 0;
        for(i = 0; i <= D; i++)
        if(C[x][i] and Dist[i] > Dist[x] + Cost[x][i])
        {
            Dist[i] = Dist[x] + Cost[x][i];
            prev[i] = x;
            if(!inq[i])
            {
                inq[i] = 1;
                Q.push(i);
            }
        }
    }
    return Dist[D] != oo;
}
int main()
{
    ifstream fi("cc.in");
    ofstream fo("cc.out");
    fi >> n;
    S = 0; D = 2*n + 1;
    for(i = 1; i <= n; i++)
    {
        C[S][i] = C[n+i][D] = 1;
        Cost[S][i] = Cost[n+i][D] = 0;
        for(j = 1; j <= n; j++)
        {
            fi >> Cost[i][n+j];
            C[i][n+j] = 1;
            Cost[n+j][i] = -Cost[i][n+j];
        }
    }
    while(Bellman())
    {
        for(i = D; i != S; i = prev[i])
            C[prev[i]][i]--, C[i][prev[i]]++;
        sol += Dist[D];
    }
    fo << sol << "\n";
    return 0;
}