Cod sursa(job #2005547)

Utilizator infomaxInfomax infomax Data 27 iulie 2017 14:04:43
Problema Cc Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <bits/stdc++.h>

using namespace std;

FILE *F=fopen("cc.in", "r"), *G=fopen("cc.out", "w");

int n, c[202][202], C[202][202], dist[202], t[202], s, d, x, y, f[202][202], ok, ans;
bitset<202> inq;
vector<int> a[202];
const int inf = 1 << 30;
queue<int> q;

int bellman()
{
    vector<int> :: iterator it;
    for(int i = s; i <= d; ++ i)
        dist[i] = inf;
    dist[s] = 0; inq[s] = 1; q.push(s);
    while(!q.empty())
    {
        x = q.front();
        inq[x] = 0;
        q.pop();
        for(it = a[x].begin(); it != a[x].end(); ++ it)
        {
            y = *it;
            if(C[x][y] > f[x][y] && dist[y] > dist[x] + c[x][y])
            {
                dist[y] = dist[x] + c[x][y]; t[y] = x;
                if(!inq[y]) inq[y] = 1, q.push(y);
            }
        }
    }
    if(dist[d] == inf) return 0;
    for(int i = d; i != s; i = t[i])
        f[t[i]][i] ++, f[i][t[i]] --;
    return dist[d];
}

int main()
{
    fscanf(F, "%d ", &n);
    for(int i = 1; i <= n; ++ i)
        for(int j = 1; j <= n; ++ j)
        {
            fscanf(F, "%d ", &x);
            a[i].push_back(j+n);
            c[i][j+n] = x;
            a[j+n].push_back(i);
            c[j+n][i] = -x;
            C[i][j+n] =  1;
        }
    s = 0; d = 2*n+1;
    for(int i = 1; i <= n; ++ i)
    {
        a[i].push_back(s);
        c[i][s] = 0;
        a[s].push_back(i);
        c[s][i] = 0;
        C[s][i] =  1;
    }
    for(int i = 1; i <= n; ++ i)
    {
        a[i+n].push_back(d);
        c[i+n][d] = 0;
        a[d].push_back(i+n);
        c[d][i+n] = 0;
        C[i+n][d] =  1;
    }
    ok = 1;
    while(ok)
        ok = bellman(), ans += ok;
    fprintf(G, "%d", ans);
    return 0;
}