Cod sursa(job #2004379)

Utilizator infomaxInfomax infomax Data 25 iulie 2017 18:31:02
Problema Cuplaj maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.29 kb
#include <bits/stdc++.h>

using namespace std;

FILE *Fi=fopen("cmcm.in", "r"), *G=fopen("cmcm.out", "w");

int s, d, dist[710], t[710], C[710][710], F[710][710], l, x, y, z, n, m, sol, ok, e, M[710][710], nr;
bitset<710> c;
queue<int> q;
vector<int> a[710];
vector<int> cost[710];
const int inf = 2000000000;

int BellmanFord()
{
    for(int i = s; i <= d; ++ i)
        dist[i] = inf, t[i] = -1;
    c = 0;
    q.push(s); c[s] = 1; dist[s] = 0;
    while(!q.empty())
    {
        x = q.front(); c[x] = 0;
        q.pop();
        l = a[x].size();
        for(int i = 0; i < l; ++ i)
        {
            y = a[x][i];
            z = cost[x][i];
            if(C[x][y] > F[x][y] && dist[y] > dist[x] + cost[x][i])
            {
                dist[y] = dist[x] + cost[x][i]; t[y] = x;
                if(!c[y]) c[y] = 1, q.push(y);
            }
        }
    }

    if(dist[d] == inf) return 0;
    int f = inf;
    for(int i = d; i != s; i = t[i])
        f = min(f, C[t[i]][i] - F[t[i]][i]);

    for(int i = d; i != s; i = t[i])
        F[t[i]][i] += f, F[i][t[i]] -= f;

    return f * dist[d];
}

int main()
{
    fscanf(Fi, "%d %d %d ", &n, &m, &e);
    for(int  i = 1; i <= e; ++ i)
    {
        fscanf(Fi, "%d %d %d ", &x, &y, &z);
        x++; y+=n+1;
        a[x].push_back(y);
        a[y].push_back(x);
        cost[y].push_back(-z);
        cost[x].push_back(z);
        M[x][y] = i;
        C[x][y] = 1;
    }
    s = 1; d = n+m+2;
    for(int i = 2; i <= n + 1; ++ i)
    {
        a[i].push_back(1);
        a[1].push_back(i);
        cost[i].push_back(0);
        cost[1].push_back(0);
        C[1][i] = 1;
    }
    for(int i = n + 2; i <= n+m+1; ++ i)
    {
        a[i].push_back(d);
        a[d].push_back(i);
        cost[d].push_back(0);
        cost[i].push_back(0);
        C[i][d] = 1;
    }
    ok = 1;
    while(ok)
    {
        ok = BellmanFord();
        sol += ok;
    }
    for(int i = 2; i <= n+1; ++ i)
        for(int j = n+2; j < d; ++ j)
            if(C[i][j] && F[i][j])
            {nr ++;break;}
    fprintf(G, "%d %d\n", nr, sol);
    for(int i = 2; i <= n+1; ++ i)
        for(int j = n+2; j < d; ++ j)
            if(C[i][j] && F[i][j])
            {fprintf(G, "%d ", M[i][j]);break;}
    return 0;
}