Pagini recente » Cod sursa (job #1822538) | Cod sursa (job #2380517) | Cod sursa (job #2676604) | Cod sursa (job #2930020) | Cod sursa (job #1679587)
#include<fstream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
ifstream fin( "critice.in" ); ofstream fout( "critice.out" );
const int nmax = 1000;
const int mmax = 10000;
int start, dest;
int c[ nmax + 1 ][ nmax + 1 ];
bool f[ nmax + 1 ], w[ nmax + 1 ];
int ant[ nmax + 1 ], init[ nmax + 1 ], q[ nmax + 1 ];
vector< int > g[ nmax + 1 ];
vector< int > ans;
struct str{
int x, y;
str() {}
str( int _x, int _y ) : x( _x ), y( _y ) {}
} muchie[ mmax + 1 ];
bool bfs() {
int first, last;
memset( f, 0, sizeof( f ) );
memset( ant, 0, sizeof( ant ) );
first = last = 0;
f[ start ] = 1;
q[ 0 ] = start;
while ( first <= last && f[ dest ] == 0 ) {
int x = q[ first ++ ];
for( vector< int >::iterator it = g[ x ].begin(); it != g[ x ].end(); ++ it ) {
if ( f[ *it ] == 0 && c[ x ][ *it ] ) {
q[ ++ last ] = *it;
f[ *it ] = 1;
ant[ *it ] = x;
}
}
}
return f[ dest ];
}
void dfs1( int nod ) {
f[ nod ] = 1;
for( vector< int >::iterator it = g[ nod ].begin(); it != g[ nod ].end(); ++ it ) {
if ( f[ *it ] == 0 && c[ nod ][ *it ] ) {
dfs1( *it );
}
}
}
void dfs2( int nod ) {
w[ nod ] = 1;
for( vector< int >::iterator it = g[ nod ].begin(); it != g[ nod ].end(); ++ it ) {
if ( w[ *it ] == 0 && c[ *it ][ nod ] ) {
dfs2( *it );
}
}
}
int main() {
int n, m;
fin >> n >> m;
start = 1; dest = n;
for( int i = 0; i < m; ++ i ) {
int x, y, z;
fin >> x >> y >> z;
muchie[ i ] = str( x, y );
g[ x ].push_back( y ); c[ x ][ y ] += z;
g[ y ].push_back( x ); c[ y ][ x ] += z;
}
while ( bfs() ) {
for( vector< int >::iterator it = g[ dest ].begin(); it != g[ dest ].end(); ++ it ) {
if ( f[ *it ] == 1 && c[ *it ][ n ] ) {
int tata, k = *it;
int sol = c[ *it ][ n ];
while ( (tata = ant[ k ]) ) {
if ( c[ tata ][ k ] < sol ) {
sol = c[ tata ][ k ];
}
k = tata;
}
c[ *it ][ n ] -= sol;
c[ n ][ *it ] += sol;
k = *it;
while ( (tata = ant[ k ]) ) {
c[ tata ][ k ] -= sol;
c[ k ][ tata ] += sol;
k = tata;
}
}
}
}
memset( f, 0, sizeof( f ) ); memset( w, 0, sizeof( w ) );
dfs1( start ); dfs2( dest );
for( int i = 0; i < m; ++ i ) {
int x = muchie[ i ].x, y = muchie[ i ].y;
if ( (!c[ x ][ y ] || !c[ y ][ x ]) && ((f[ x ] && w[ y ]) || (w[ x ] && f[ y ])) ) {
ans.push_back( i + 1 );
}
}
fout << ans.size() << "\n";
for( int i = 0; i < ( int )ans.size(); ++ i ) {
fout << ans[ i ] << "\n";
}
fin.close();
fout.close();
return 0;
}