Cod sursa(job #754854)

Utilizator Theorytheo .c Theory Data 3 iunie 2012 21:03:49
Problema Critice Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include<fstream>
#include<vector>
#include<stdlib.h>
#define nmax 1<<10
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
int N, M, vis[nmax], v[nmax] ,TT[nmax], C[nmax][nmax], F[nmax][nmax], aux[nmax][nmax], c[nmax];
vector <int> G[nmax];

int bfs()
{
    int nod, i, j, V;
    for( i = 0; i <= N ; ++i, v[i] = 0);
    v[1]= 1;
    c[1] = c[0] = 1;
    for(i = 1; i <= c[0]; ++i)
    {
        nod = c[i];
        if(nod != N)
        for( j = 0; j < G[nod].size(); ++j)
        {
            V = G[nod][j];
            if(C[nod][V] > F[nod][V] && !v[V])


                c[++c[0]] = V,
                v[V] = 1,
                TT[V] = nod;

        }
    }
    return v[N];

}

void solve()
{
    int i, flux_min = 0 , j, nod;
    for(; bfs(); )
    {
        for(j = 0 ; j < G[N].size(); ++j)
        {
            nod = G[N][j] ;
            if(C[nod][N] > F[nod][N] && v[nod]) {
                 flux_min = 100000000;

                for(nod = N; nod != 1 ; nod = TT[nod] )
                    flux_min = min(flux_min, C[TT[nod]][nod] - F[TT[nod]][nod]);

                for(nod = N; nod!= 1; nod = TT[nod])
                    F[TT[nod]][nod] += flux_min , F[nod][TT[nod]] -= flux_min;
            }
        }
    }
}

void dfs(int x, int y)
{
    for(int i = 0 ;i  < G[x].size(); ++i)

        if(C[x][G[x][i]] - F[x][G[x][i]] && !vis[G[x][i]] )
            vis[G[x][i]] = y, dfs(G[x][i], y);

}
void read()
{

    fin >> N >> M;
    for(int i = 1; i <= M; ++i)
    {
        int x, y, z;
        fin >> x>> y>> z;
        C[x][y] += z;
        C[y][x] += z;
        aux[x][y] = i;
        aux[y][x] = i;
        G[x].push_back(y);
        G[y].push_back(x);

    }
    solve();
}
int main()
{
    int sol[10002], Nr = 0;
    read();
    solve();
    vis[1] = 1;
    vis[N] = 2;
    dfs(1,1);
    dfs(N,2);

    for(int  i =1; i <= N; i++)
        for(int j = 0 ;j < G[i].size(); ++j)
        if(C[i][G[i][j]] && (C[i][G[i][j]] == F[i][G[i][j]] || C[i][G[i][j]] + F[i][G[i][j]] == 0) && vis[i] == 1 && vis[G[i][j]] == 2)
            sol[++Nr] = aux[i][G[i][j]] ;
    fout << Nr <<'\n' ;
    for(int i = 1; i <= Nr; ++i)
        fout << sol[i] <<'\n';
    fin.close();
    return 0;

}