Cod sursa(job #1277337)

Utilizator lokixdSebastian lokixd Data 27 noiembrie 2014 16:06:19
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.62 kb
#include <cstdio>
#include <utility>
#include <cstring>
#include <algorithm>
using namespace std;
 
#define N 1005
 
int n , m , i , j , x , y , z      ;
int capacitate[N][N] , flux[N][N] , st[N] , ant[N] , sss[N]  ;
bool use[N];
pair<int,int> drum[10005];
 
struct str{
    int val;
    str *next;
};
str *vecini[N],*tmp;
 
void add(int x,int y){
    tmp       = new str;
    tmp->val  = y;
    tmp->next = vecini[x];
    vecini[x] = tmp;
}
 
void DFS(int x,int i){
    str *tmp;
    for( tmp = vecini[x]; tmp ; tmp = tmp->next ){
        if( sss[ tmp->val ] || capacitate[x][tmp->val] == flux[x][tmp->val] ) continue;
        sss[ tmp->val]= i ;
        DFS( tmp->val , i);
    }
}
 
void BFS(){
    st[0] = st[1] = 1;
    memset( use , 0 , sizeof(use) );
    memset( ant , 0 , sizeof(ant) );
    use[1] = true;
    for(int i = 1; i <= st[0]; ++i){
        if( st[i] == n ) continue ;
        x = st[i];
        for( tmp = vecini[x] ; tmp ; tmp = tmp->next ){
            y = tmp->val;
            if( use[y] || capacitate[x][y] == flux[x][y] ) continue;
            st[ ++st[0] ] = y;
            use[y] = true;
            ant[y] = x;
        }
    }
 
}
 
int main()
{
    freopen( "critice.in"  , "r" , stdin  );
    freopen( "critice.out" , "w" , stdout );
 
    scanf( "%d %d\n" , &n , &m );
 
    for(i=1;i<=m;++i){
        scanf( "%d %d %d\n" , &x , &y , &z);
        capacitate[x][y] = z;
        capacitate[y][x] = z;
        add( x , y );
        add( y , x );
        drum[i] = { x , y };
    }
    int res;
    for( BFS() ; use[n] ; BFS() )
        for( tmp = vecini[n]; tmp ; tmp = tmp->next ){
            x = tmp->val;
            res = capacitate[x][n] - flux[x][n];
            if( !use[x] || res == 0 ) continue;
            while( ant[x] ){
                res = min( res , capacitate[ ant[x] ][x] - flux[ ant[x] ][x] );
                x = ant[x];
            }
            x = tmp->val;
            flux[x][n] += res;
            flux[n][x] -= res;
            while( ant[x] ){
                flux[ ant[x] ][x] += res;
                flux[x][ ant[x] ] -= res;
                x = ant[x];
            }
        }
    sss[1] = 1 ;
    sss[n] = 2 ;
    DFS(1 ,  1);
    DFS(n ,  2);
    st[0] = 0;
    for( i=1 ; i<=m ; ++i ){
        if( capacitate[ drum[i].first ][ drum[i].second ] == abs( flux[ drum[i].first ][ drum[i].second ] )
         && ( sss[ drum[i].first ] + sss[ drum[i].second ] ) == 3 )
            st[ ++st[0] ] = i;
    }
    printf("%d\n",st[0]);
    for( i = 1; i <= st[0] ; ++i )
        printf( "%d\n" , st[i] );
 
    return 0;
}