Cod sursa(job #791592)

Utilizator visanrVisan Radu visanr Data 24 septembrie 2012 17:19:47
Problema Traseu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.97 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

#define pb push_back
#define oo 0x3f3f3f3f
#define nmax 70

vector<int> G[nmax];
int Father[nmax], Dist[nmax], Cost[nmax][nmax], DegIn[nmax], DegOut[nmax];
int Capacity[nmax][nmax], CurrentFlow[nmax][nmax];
int N, M, X, Y, C, S, D, maxFlow, minFlow;
bool InQueue[nmax];
queue<int> Q;


bool BellmanFord()
{
    for(int i = S; i <= D; i++)
        Dist[i] = oo, Father[i] = -1, InQueue[i] = 0;
    Dist[S] = 0;
    Father[S] = S;
    InQueue[S] = 1;
    Q.push(S);
    while(!Q.empty())
    {
        int node = Q.front(); Q.pop(); InQueue[node] = false;
        for(vector<int> :: iterator it = G[node].begin(); it != G[node].end(); ++it)
            if(Dist[*it] > Dist[node] + Cost[node][*it] && Capacity[node][*it] > CurrentFlow[node][*it])
            {
                Dist[*it] = Dist[node] + Cost[node][*it];
                Father[*it] = node;
                if(!InQueue[*it])
                {
                    InQueue[*it] = 1;
                    Q.push(*it);
                }
            }
    }
    return (Dist[D] != oo);
}

int main()
{
    freopen("traseu.in", "r", stdin);
    freopen("traseu.out", "w", stdout);
    int i, j, k;
    scanf("%i %i", &N, &M);
    for(i = 1; i <= N; i++)
        for(j = 1; j <= N; j++)
            Cost[i][j] = oo;
    for(i = 1; i <= M; i++)
    {
        scanf("%i %i %i", &X, &Y, &C);
        Cost[X][Y] = C;
        DegOut[X] ++;
        DegIn[Y] ++;
        maxFlow += Cost[X][Y];
    }
    for(k = 1; k <= N; k++)
        for(i = 1; i <= N; i++)
            for(j = 1; j <= N; j++)
                if(i != k && j != k && Cost[i][k] + Cost[k][j] < Cost[i][j])
                    Cost[i][j] = Cost[i][k] + Cost[k][j];
    S = 0, D = N + 1;
    for(i = 1; i <= N; i++)
    {
        if(DegIn[i] > DegOut[i])
        {
            G[S].pb(i), G[i].pb(S);
            Capacity[S][i] = DegIn[i] - DegOut[i];
        }
        if(DegOut[i] > DegIn[i])
        {
            G[i].pb(D), G[D].pb(i);
            Capacity[i][D] = DegOut[i] - DegIn[i];
        }
    }
    for(i = 1; i <= N; i++)
        for(j = 1; j <= N; j++)
            if(DegIn[i] > DegOut[i] && DegOut[j] > DegIn[j])
            {
                G[i].pb(j), G[j].pb(i);
                Capacity[i][j] = oo;
                Cost[j][i] = -Cost[i][j];
            }
    int now = 0;
    while(BellmanFord())
    {
        int minFlow = oo;
        for(int node = D; node != S; node = Father[node])
            minFlow = min(minFlow, Capacity[Father[node]][node] - CurrentFlow[Father[node]][node]);
        for(int node = D; node != S; node = Father[node])
        {
            CurrentFlow[Father[node]][node] += minFlow;
            CurrentFlow[node][Father[node]] -= minFlow;
        }
        maxFlow += Dist[D] * minFlow;
    }
    printf("%i\n", maxFlow);
    return 0;
}