Cod sursa(job #1708519)

Utilizator tudormaximTudor Maxim tudormaxim Data 27 mai 2016 11:17:28
Problema Critice Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.83 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#include <bitset>
using namespace std;

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

const int nmax = 1005;
const int oo = 1<<29;
vector <int> g[nmax], sol;
vector < pair<int,int> > edges;
bitset <nmax> viz;
int n, m, cap[nmax][nmax], flow[nmax][nmax];
int dady[nmax];

inline bool EK_bfs(int source, int dest) {
    queue <int> q;
    int dad;
    viz = 0;
    viz[source] = true;
    q.push(source);
    while (!q.empty()) {
        dad = q.front();
        q.pop();
        if (dad == dest) continue;
        for (auto son : g[dad]) {
            if (viz[son] == false && cap[dad][son] > flow[dad][son]) {
                viz[son] = true;
                dady[son] = dad;
                q.push(son);
            }
        }
    }
    return viz[dest];
}

void EK(int source, int dest) {
    int max_flow = -oo, floww, node;
    while (EK_bfs(source, dest)) {
        for (auto it : g[dest]) {
            if (viz[it] == true && flow[it][dest] != cap[it][dest]) {
                floww = oo;
                for (node = it; node != source; node = dady[node]) {
                    floww = min(floww, cap[dady[node]][node] - flow[dady[node]][node]);
                }
                if (floww == 0) continue;
                max_flow += floww;
                flow[it][dest] += floww;
                flow[dest][it] -= floww;
                for (node = it; node != source; node = dady[node]) {
                    flow[dady[node]][node] += floww;
                    flow[node][dady[node]] -= floww;
                }
            }
        }
    }
}

bitset <nmax> bfs(int start) {
    queue <int> q;
    bitset <nmax> mark = 0;
    int dad;
    mark[start] = true;
    q.push(start);
    while (!q.empty()) {
        dad = q.front();
        q.pop();
        for (auto son : g[dad]) {
            if (mark[son] == false && cap[dad][son] != flow[dad][son] && cap[son][dad] != flow[son][dad]) {
                mark[son] = true;
                q.push(son);
            }
        }
    }
    return mark;
}

int main() {
    ios_base :: sync_with_stdio(false);
    bitset <nmax> a, b;
    int x, y, c, i;
    fin >> n >> m;
    for (i = 1; i <= m; i++) {
        fin >> x >> y >> c;
        g[x].push_back(y);
        g[y].push_back(x);
        cap[x][y] = cap[y][x] = c;
        edges.push_back({x, y});
    }
    EK(1, n);
    a = bfs(1);
    b = bfs(n);
    for (i = 0; i < m; i++) {
        if ((a[edges[i].first] && b[edges[i].second]) || (a[edges[i].second] && b[edges[i].first])) {
            sol.push_back(i+1);
        }
    }
    fout << sol.size() << "\n";
    for (auto it : sol) {
        fout << it << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}