Cod sursa(job #2615981)

Utilizator zhm248Mustatea Radu zhm248 Data 16 mai 2020 01:23:57
Problema Taramul Nicaieri Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.92 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, flow;
        vector<pair<int, int>> result;
        queue<int> nodes, flows;
        vector<int> parents((n << 1) + 2, -1);
        while (1) {
            while (nodes.size()) {
                nodes.pop();
                flows.pop();
            }
            for (i = 0; i <= (n << 1) + 1; ++i) {
                parents[i] = -1;
            }
            parents[0] = 0;
            nodes.push(0);
            flows.push(INF);
            while (nodes.size()) {
                node = nodes.front();
                flow = flows.front();
                if (node == (n << 1) + 1) {
                    do {
                        C[node][parents[node]] += C[parents[node]][node] < 0 ? -flow : flow;
                        C[parents[node]][node] += C[parents[node]][node] < 0 ? flow : -flow;
                        node = parents[node];
                    } while (node);
                    break;
                }
                nodes.pop();
                flows.pop();
                for (i = 0; i <= (n << 1) + 1; ++i) {
                    if (parents[i] == -1 && C[node][i]) {
                        parents[i] = node;
                        nodes.push(i);
                        flows.push(min(flow, abs(C[node][i])));
                    }
                }
            }
            if (nodes.empty()) {
                break;
            }
        }
        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;
}