Pagini recente » Cod sursa (job #228835) | Cod sursa (job #792946)
Cod sursa(job #792946)
#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[], int Dir) {
queue<int> Q;
for (InitBFS(Start, Q, Father); !Q.empty(); Q.pop()) {
int X = Q.front();
if (X == End)
continue;
for (vector<int>::iterator Y = G[X].begin(); Y != G[X].end(); ++Y)
if (Father[*Y] == -1 && ((Dir == 0 && Cap[X][*Y] > Flow[X][*Y]) || (Dir == 1 && Cap[*Y][X] > Flow[*Y][X])))
Q.push(*Y), Father[*Y] = X;
}
return Father[End] != -1;
}
void EdmondsKarp() {
int MaxFlow = 0;
for (int CFlow; BFS(S, D, F, 0); MaxFlow += CFlow) {
for (vector<int>::iterator Y = G[D].begin(); Y != G[X].end(); ++Y) {
if (Father[*Y] == -1 || Flow[*Y][D] >= Cap[*Y][D])
continue;
Father[D] = *Y;
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, D, FS, 0); BFS(D, S, FD, 1);
for (unsigned i = 0; i < E.size(); ++i) {
int X = E[i].x, Y = E[i].y;
if ((FS[X] != -1 && FD[Y] != -1 && Flow[X][Y] == Cap[X][Y]) || (FS[Y] != -1 && FD[X] != -1 && Flow[Y][X] == Cap[Y][X]))
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();
}