Cod sursa(job #1594343)

Utilizator radu_uniculeu sunt radu radu_unicul Data 9 februarie 2016 14:08:22
Problema Taramul Nicaieri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#define oo 0x3f3f3f3f

using namespace std;

ifstream fin("harta.in");
ofstream fout("harta.out");

const int MAXN = 206;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

int N, M, X, Y, C[MAXN][MAXN], F[MAXN][MAXN], Father[MAXN], maxFlow;
Graph G;
bitset <MAXN> Used;
queue<int>Q;

inline bool BFs() {
    Used.reset();
    for(Q.push(0), Used[0] = true ; !Q.empty() ; Q.pop()){
        int Node = Q.front();
        for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it)
            if(!Used[*it] && C[Node][*it] > F[Node][*it])
                Q.push(*it), Used[*it] = true, Father[*it] = Node;
    }
    return Used[M];
}

int main() {
    fin >> N;
    M = 2 * N + 1;
    for(int i = 1 ; i <= N ; ++ i) {
        int x, y;
        fin >> x >> y; X += x; Y += y;
        G[0].push_back(i);
        C[0][i] = x;
        G[i+N].push_back(M);
        C[i+N][M] = y;
    }
    for(int i = 1 ; i <= N ; ++ i)
        for(int j = N + 1 ; j < M ; ++ j)
            if(i != j - N) {
                C[i][j] = 1;
                G[i].push_back(j);
                G[j].push_back(i);
            }
    for( ; BFs() ; ) {
        ++ maxFlow;
        for(int i = M ; i ; i = Father[i])
            ++ F[Father[i]][i],
            -- F[i][Father[i]];
    }
    fout << maxFlow << '\n';
    for(int i = 1 ; i <= N ; ++ i)
        for(int j = N + 1; j < M ; ++ j)
            if(F[i][j] == 1)
                fout << i << ' ' << j - N  << '\n';
    fin.close();
    fout.close();
    return 0;
}