Pagini recente » Cod sursa (job #115074) | Cod sursa (job #1631260) | Cod sursa (job #2477702) | Cod sursa (job #1084474) | Cod sursa (job #1069294)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
const int inf= 1<<30;
const int nmax= 1000;
const int mmax= 10000;
int n, m;
vector <int> v[nmax+1];
int c[nmax+1][nmax+1], f[nmax+1][nmax+1], p[nmax+1], sol[mmax+1];
bool u[nmax+1];
struct edge {
int x, y;
} a[mmax+1];
int bfs( ) {
for ( int i= 0; i<=nmax; ++i ) {
u[i]= 0;
}
u[1]= 1;
queue <int> q;
q.push(1);
while ( !q.empty() ) {
int x= q.front();
if ( x<n ) {
for ( int i= 0; i<(int)v[x].size(); ++i ) {
int y= v[x][i];
if ( c[x][y]!=f[x][y] && u[y]==0 ) {
u[y]= 1;
q.push(y);
p[y]= x;
}
}
}
q.pop();
}
return u[n];
}
bool u1[nmax+1], u2[nmax+1];
void dfs ( int x, bool used[] ) {
used[x]= 1;
for ( int i= 0; i<(int)v[x].size(); ++i ) {
int y= v[x][i];
if ( f[x][y]!=c[x][y] && f[y][x]!=c[y][x] && used[y]==0 ) {
dfs(y, used);
}
}
}
void maxflow( ) {
while ( bfs() ) {
for ( int i= 0; i<(int)v[n].size(); ++i ) {
int x= v[n][i];
if ( f[x][n]!=c[x][n] && u[x]==1 ) {
p[n]= x;
int fmin= inf;
for ( x= n; x!= 1; x= p[x] ) {
if ( c[p[x]][x]-f[p[x]][x]<fmin ) {
fmin= c[p[x]][x]-f[p[x]][x];
}
}
for ( x= n; x!= 1; x= p[x] ) {
f[p[x]][x]+= fmin;
f[x][p[x]]-= fmin;
}
}
}
}
}
int main( ) {
fin>>n>>m;
for ( int i= 1; i<=m; ++i ) {
int x, y, z;
fin>>x>>y>>z;
v[x].push_back(y);
v[y].push_back(x);
c[x][y]= c[y][x]= z;
a[i].x= x;
a[i].y= y;
}
maxflow();
dfs(1, u1), dfs(n, u2);
int k= 0;
for ( int i= 1; i<=m; ++i ) {
int x= a[i].x, y= a[i].y;
if ( ( f[x][y]==c[x][y] && u1[x]==1 && u2[y]==1 ) || ( f[y][x]==c[y][x] && u1[y]==1 && u2[x]==1 ) ) {
++k;
sol[k]= i;
}
}
fout<<k<<"\n";
for ( int i= 1; i<=k; ++i ) {
fout<<sol[i]<<"\n";
}
return 0;
}