Pagini recente » Cod sursa (job #1881450) | Cod sursa (job #428112) | Cod sursa (job #590477) | Cod sursa (job #1561611) | Cod sursa (job #2750609)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
const int nmax = 1000;
const int mmax = 10000;
vector <int> g[nmax+1];
int e[mmax+1][2];
int r[nmax+1];
queue <int> q;
int f[nmax+1][nmax+1];
int u[nmax+1];
int dfs_mark;
void dfs( int x ) {
u[x] = dfs_mark;
for ( int i = 0; i < int(g[x].size()); ++ i ) {
int xn = g[x][i];
if ( f[x][xn] > 0 && f[xn][x] > 0 && u[xn] == 0 ) {
dfs(xn);
}
}
}
int main( ) {
int n, m;
fin >> n >> m;
for ( int i = 1; i <= m; ++ i ) {
int x, y, z;
fin >> x >> y >> z;
g[x].push_back(y);
g[y].push_back(x);
f[x][y] = z;
f[y][x] = z;
e[i][0] = x;
e[i][1] = y;
}
r[n] = -1;
while ( r[n] != 0 ) {
for ( int i = 1; i <= n; ++ i ) {
r[i] = 0;
}
r[1] = -1;
q.push(1);
while ( q.empty() == 0 ) {
int x = q.front();
q.pop();
for ( int i = 0; i < int(g[x].size()); ++ i ) {
int xn = g[x][i];
if ( f[x][xn] > 0 && r[xn] == 0 ) {
r[xn] = x;
q.push(xn);
}
}
}
if ( r[n] != 0 ) {
for ( int i = 0; i < int(g[n].size()); ++ i ) {
int x = g[n][i];
int aux = f[x][n];
while ( r[x] != -1 && aux > 0 ) {
if ( f[r[x]][x] < aux ) {
aux = f[r[x]][x];
}
x = r[x];
}
if ( aux > 0 ) {
x = g[n][i];
f[x][n] -= aux;
f[n][x] += aux;
while ( r[x] != -1 ) {
f[r[x]][x] -= aux;
f[x][r[x]] += aux;
x = r[x];
}
}
}
}
}
dfs_mark = 1;
dfs(1);
dfs_mark = 2;
dfs(n);
vector <int> sol;
for ( int i = 1; i <= m; ++ i ) {
int x = e[i][0], y = e[i][1];
if ( u[x]+u[y] == 3 ) {
sol.push_back(i);
}
}
fout << sol.size() << "\n";
for ( int i = 0; i < int(sol.size()); ++ i ) {
fout << sol[i] << "\n";
}
return 0;
}