Cod sursa(job #1290620)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 11 decembrie 2014 16:28:58
Problema Cuplaj maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.28 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "cmcm";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 60) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = 666013;

const int NMAX = 2 * 300 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;

int N, M, E;
int source, sink;
vector<int> V[NMAX];
int Cost[NMAX][NMAX];
int F[NMAX][NMAX];
int C[NMAX][NMAX];
int dist[NMAX];
int dad[NMAX];
int edge[NMAX][NMAX];
int minCost, maxFlow;
deque<int> Q;
bitset<NMAX> viz;

int bf() {
    int i, x;

    for(i = 1; i <= N + M + 1; i++)
        dist[i] = INF;

    viz = 0;
    Q.push_back(0);
    viz[0] = 1;

    while(!Q.empty()) {
        x = Q.front();
        viz[x] = 0;
        Q.pop_front();

        for(auto y : V[x])
            if(F[x][y] < C[x][y] && dist[x] + Cost[x][y] < dist[y]) {
                dist[y] = dist[x] + Cost[x][y];

                if(!viz[y]) {
                    Q.push_back(y);
                    viz[y] = 1;
                }

                dad[y] = x;
            }
    }

    return (dist[sink] != INF);
}

int main() {
    int x, y, z, v, i, j;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%d%d%d", &N, &M, &E);

    for(i = 1; i <= E; i++) {
        scanf("%d%d%d", &x, &y, &z);
        Cost[x][y + N] = z;
        Cost[y + N][x] = -z;
        C[x][y + N] = 1;
        V[x].push_back(y + N);
        V[y + N].push_back(x);
        edge[x][y + N] = edge[y + N][x] = i;
    }

    source = 0;
    sink = N + M + 1;

    for(i = 1; i <= N; i++) {
        V[source].push_back(i);
        V[i].push_back(source);
        C[source][i] = 1;
    }

    for(i = N + 1; i <= N + M; i++) {
        V[i].push_back(sink);
        V[sink].push_back(i);
        C[i][sink] = 1;
    }

    while(bf()) {
        v = INF;

        for(i = sink; i != source; i = dad[i])
            v = min(v, C[dad[i]][i] - F[dad[i]][i]);

        for(i = sink; i != source; i = dad[i]) {
            F[dad[i]][i] += v;
            F[i][dad[i]] -= v;
        }

        maxFlow += v;
        minCost += dist[sink];
    }

    printf("%d %d\n", maxFlow, minCost);

    for(i = 1; i <= N; i++)
        for(j = N + 1; j <= N + M; j++)
            if(F[i][j])
                printf("%d ", edge[i][j]);

    return 0;
}