Cod sursa(job #2632034)

Utilizator cyg_vladioanBirsan Vlad cyg_vladioan Data 1 iulie 2020 23:53:50
Problema Cc Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.42 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int NMAX = 200;
const int INF = 2.e9;
int cst[NMAX + 5][NMAX + 5] , cf[NMAX + 5][NMAX + 5] , d[NMAX + 5] , dist[NMAX + 5] , new_dist[NMAX + 5] , p[NMAX + 5] , n , s , t;
vector <int> G[NMAX + 5];
struct path
{
    int nod , cost;
    path(int x = 0 , int y = 0)
    {
        nod = x;
        cost = y;
    }
};
bool operator<(const path& a , const path& b)
{
    return a.cost > b.cost;
}
int dijkstra()
{
    int u , v , i , cost , new_cost;
    for(i = 0 ; i <= t ; i ++)
        d[i] = INF;
    d[s] = 0;
    priority_queue <path> pq;
    pq.push(path(s , 0));
    while(!pq.empty())
    {
        u = pq.top().nod;
        cost = pq.top().cost;
        pq.pop();
        if(d[u] < cost)
            continue;
        for(i = 0 ; i < G[u].size() ; i ++)
        {
            v = G[u][i];
            if(cf[u][v])
            {
                new_cost = d[u] + cst[u][v];
                if(new_cost < d[v])
                {
                    d[v] = new_cost;
                    p[v] = u;
                    pq.push(path(v , new_cost));
                }
            }
        }
    }
    if(d[t] != INF)
        return 1;
    return 0;
}
int get_flow()
{
    int flow , nod;
    flow = INF;
    for(nod = t ; nod != s ; nod = p[nod])
        flow = min(flow , cf[p[nod]][nod]);
    return flow;
}
int set_flow(int flow)
{
    for(int nod = t ; nod != s ; nod = p[nod])
    {
        cf[p[nod]][nod] -= flow;
        cf[nod][p[nod]] += flow;
    }
    return flow * d[t];
}
int fmcm()
{
    int cost = 0;
    while(dijkstra())
        cost += set_flow(get_flow());
    return cost;
}
int main()
{
    freopen("cc.in" , "r" , stdin);
    freopen("cc.out" , "w" , stdout);
    int i , j , x;
    scanf("%d" , &n);
    for(i = 1 ; i <= n ; i ++)
        for(j = 1 ; j <= n ; j ++)
        {
            scanf("%d" , &x);
            G[i].push_back(j + n);
            G[j + n].push_back(i);
            cf[i][j + n] = 1;
            cst[i][j + n] = x;
            cst[j + n][i] = -x;
        }
    s = 0;
    t = 2 * n + 1;
    for(i = 1 ; i <= n ; i ++)
    {
        G[0].push_back(i);
        G[i].push_back(0);
        G[i + n].push_back(t);
        G[t].push_back(i + n);
        cf[0][i] = cf[i + n][t] = 1;
    }
    printf("%d\n" , fmcm());
    return 0;
}