Cod sursa(job #792598)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 27 septembrie 2012 21:09:42
Problema Critice Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.02 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <cassert>

#define x first
#define y second

using namespace std;

const int MaxN = 1005;
const int oo = 100000;

vector<int> G[MaxN];
int N, S, D, Cap[MaxN][MaxN], Flow[MaxN][MaxN], F[MaxN];
int FS[MaxN], FD[MaxN];
vector< pair<int, int> > E;
vector<int> Sol;

inline void InitBFS(int Start, queue<int> &Q, int Father[]) {
    for (int X = 1; X <= N; ++X)
        Father[X] = -1;
    Q.push(Start), Father[Start] = Start;
}

bool BFS(int Start, int End, int Father[]) {
    queue<int> Q;
    for (InitBFS(Start, Q, Father); !Q.empty(); Q.pop()) {
        int X = Q.front();
        if (X == End)
            return true;
        for (vector<int>::iterator Y = G[X].begin(); Y != G[X].end(); ++Y)
            if (Father[*Y] == -1 && Cap[X][*Y] > Flow[X][*Y])
                Q.push(*Y), Father[*Y] = X;
    }
    return false;
}

void EdmondsKarp() {
    int MaxFlow = 0;
    for (int CFlow; BFS(S, D, F); MaxFlow += CFlow) {
        CFlow = oo;
        for (int X = D; X != S; X = F[X])
            CFlow = min(CFlow, Cap[F[X]][X]-Flow[F[X]][X]);
        for (int X = D; X != S; X = F[X])
            Flow[F[X]][X] += CFlow, Flow[X][F[X]] -= CFlow;
    }
}

void Solve() {
    EdmondsKarp();
    BFS(S, 0, FS); BFS(D, 0, FD);
    for (unsigned i = 0; i < E.size(); ++i)
        if ((FS[E[i].x] != -1 && FD[E[i].y] != -1) || (FS[E[i].y] != -1 && FD[E[i].x] != -1))
            Sol.push_back(i+1);
}

void Read() {
    assert(freopen("critice.in", "r", stdin));
    int M; assert(scanf("%d %d", &N, &M) == 2);
    S = 1, D = N;
    for (; M; --M) {
        int X, Y, C; assert(scanf("%d %d %d", &X, &Y, &C) == 3);
        G[X].push_back(Y), G[Y].push_back(X);
        Cap[X][Y] = Cap[Y][X] = C;
        E.push_back(make_pair(X, Y));
    }
}

void Print() {
    assert(freopen("critice.out", "w", stdout));
    printf("%d\n", static_cast<int>(Sol.size()));
    for (unsigned i = 0; i < Sol.size(); ++i)
        printf("%d\n", Sol[i]);
}

int main() {
    Read();
    Solve();
    Print();
}