Cod sursa(job #2615982)

Utilizator zhm248Mustatea Radu zhm248 Data 16 mai 2020 01:48:10
Problema Taramul Nicaieri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.13 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cmath>
#include <algorithm>
#define INF 1000000
using namespace std;

const int kNmax = 300;

class Task {
 public:
    void solve() {
        read_input();
        print_output(get_result());
    }

 private:
    int n;
    int m;
    int sum = 0;
    int C[kNmax][kNmax];

    void read_input() {
        ifstream fin("harta.in");
        fin >> n;

        int i, j, x, y;
        memset(C, 0, sizeof(C));
        for (i = 1; i <= n; ++i) {
            fin >> x >> y;
            sum += x;
            C[0][i] = x;
            C[n + i][(n << 1) + 1] = y;
        }
        for (i = 1; i <= n; ++i) {
            for (j = 1; j <= n; ++j) {
                if (i != j) {
                    C[i][n + j] = 1;
                    C[n + i][j] = 0;
                }
            }
        }
        fin.close();
    }

    vector<pair<int, int>> get_result() {
        int i, j, node, dest = (n << 1) + 1, flow;
        vector<pair<int, int>> result;
        queue<int> nodes, flows;
        vector<int> parents(dest + 1, -1);
        while (1) {
            while (nodes.size()) {
                nodes.pop();
            }
            for (i = 0; i <= dest; ++i) {
                parents[i] = -1;
            }
            parents[0] = 0;
            nodes.push(0);
            while (nodes.size()) {
                node = nodes.front();
                nodes.pop();
                if (node == dest) {
                    continue;
                }
                for (i = 1; i <= dest; ++i) {
                    if (parents[i] == -1 && C[node][i]) {
                        parents[i] = node;
                        nodes.push(i);
                    }
                }
            }
            if (parents[dest] == -1) {
                break;
            }
            for (i = 1; i <= n; ++i) {
                node = n + i;
                if (parents[node] == -1 || !C[node][dest]) {
                    continue;
                }
                parents[dest] = node;
                flow = INF;
                for (j = dest; j; j = parents[j]) {
                    flow = min(flow, C[parents[j]][j]);
                }
                if (!flow) {
                    continue;
                }
                for (j = dest; j; j = parents[j]) {
                    C[j][parents[j]] += C[parents[j]][j] > 0 ? flow : -flow;
                    C[parents[j]][j] += C[parents[j]][j] > 0 ? -flow : flow;
                }
            }
        }
        for (i = 1; i <= n; ++i) {
            for (j = 1; j <= n; ++j) {
                if (i != j && !C[i][n + j]) {
                    result.emplace_back(make_pair(i, j));
                }
            }
        }
        return result;
    }

    void print_output(vector<pair<int, int>> result) {
        ofstream fout("harta.out");
        fout << sum << endl;
        for (auto it = result.begin(); it != result.end(); ++it) {
            fout << it->first << " " << it->second << endl;
        }
        fout.close();
    }
};

int main() {
    Task *task = new Task();
    task->solve();
    delete task;
    return 0;
}