Cod sursa(job #245627)

Utilizator alexeiIacob Radu alexei Data 18 ianuarie 2009 13:48:50
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include<cstdio>
#include<vector>
#include<queue>
#define NMAX 1024
#define inf 0x3f3f3f3f
using namespace std;

vector< pair< int, int > >G[NMAX];

int C[NMAX][NMAX],F[NMAX][NMAX],V[10004];
int Q[NMAX],T[NMAX];
bool viz[NMAX];
int FA[ 10000 ];
int N,M,FANS;
//hungry..
bool BF()
{
	int nod,q;
	vector< pair< int, int > >::iterator it;

	int inc=0,sf=1;
	Q[1]=1;

	memset( viz, 0 , sizeof( viz ) );
	memset( T, 0, sizeof( T ) );

	viz[1]=1;
	while( inc!=sf )
	{
		if( Q[++inc]==N ) continue;	
		nod=Q[inc];

		for(it=G[nod].begin(); it!=G[nod].end(); ++it)
		{
			q=it->first;

			if( viz[ q ] || C[ nod ][ q ]==F[ nod ][ q ] ) continue;
				
			viz[ q ]=1; Q[ ++sf ]=q; T[ q ]=nod;
		}
	}

return viz[N];
}

void flux()
{
	vector< pair< int, int > >::iterator it;
	int nod,flow,aux,q;
	
	while( BF() )
	{
		for( it=G[N].begin(); it!=G[N].end(); ++it )
		{
			q=it->first;	
			if( !viz[ q ] || C[ q ][ N ]==F[ q ][ N ] ) continue;

			T[N]=q;flow=inf;	
			for( nod=N; nod!=1; nod=T[nod] )
			{
				aux=C[ T[nod] ][ nod ]-F[ T[nod] ][ nod ];
				flow=flow<aux?flow:aux;
			}

			for( nod=N; nod!=1; nod=T[nod] )
			{
				F[ nod ][ T[ nod ] ]-=flow;
				F[ T[ nod ] ][ nod ]+=flow;
			}
		}
	}
}

void smen(const int S,const int level)
{
	int nod,q;
	vector< pair< int, int > >::iterator it;
	
	memset( viz, 0 , sizeof( viz ) );

	int inc=0,sf=1,i;
	Q[ 1 ]=S,viz[S]=1;

	while( inc!=sf )
	{
		nod=Q[++inc];

		for( it=G[nod].begin(); it!=G[nod].end(); ++it )
		{
			q=it->first;
			if( !viz[ q ] )
			{
				if( ( level==1 && F[ nod ][ q ]==C[ nod ][ q ] ) || ( level==2 && F[ q ][ nod ]==C[ q ][ nod ] ) )
				{	V[ it->second ]+=level; 
					if( V[ it->second ]==3 ) {
						++FANS;FA[++FA[0]]=it->second;}
				}
				else
				{
					Q[++sf]=q;
					viz[q]=1;
				}
			}
		}
	}
}

int main()
{
	freopen("critice.in","r",stdin);
	freopen("critice.out","w",stdout);
	
	scanf("%d%d\n",&N,&M);

	int i,a1,a2,a3;
	for(i=1; i<=M; ++i){
		scanf("%d%d%d\n",&a1,&a2,&a3);
		G[a1].push_back( make_pair(a2,i) );
		G[a2].push_back( make_pair(a1,i) );
		C[ a1 ][ a2 ]+=a3;
		C[ a2 ][ a1 ]+=a3;
	}

	flux();
	smen(1,1);
	smen(N,2);

	printf("%d\n",FANS);
	
	for(i=1; i<=FA[0]; ++i)
		printf("%d\n",FA[i]);

	return 0;
}