Cod sursa(job #1879986)

Utilizator DobosDobos Paul Dobos Data 15 februarie 2017 12:19:53
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.14 kb
#include <bits/stdc++.h>
#define NMAX 1005
#define INF 1e9
using namespace std;

ifstream fin("critice.in");
ofstream fout("critice.out");

struct point{
    int f,p;
    bool b;
};

vector < int > G[NMAX];


point F[NMAX][NMAX];
int ant[NMAX];
bool B[NMAX];

inline bool BFS(const int &N){
    int nod;
    deque < int > Q;
    memset(B,0,sizeof(B));
    B[1] = 1;
    Q.push_back(1);

    while(!Q.empty()){
        nod = Q.front();
        Q.pop_front();

        for(auto const it : G[nod]){
            if(B[it] == 0 && F[nod][it].f > 0){

                B[it] = 1;
                ant[it] = nod;
                if(it == N)
                    return 1;
                Q.push_back(it);
            }
        }
    }

    return 0;

}

void DFS(int nod){
    for(auto const &it : G[nod]){
        if(F[nod][it].f != 0 && F[nod][it].b == 0){
            F[nod][it].b = 1;
            DFS(it);
        }
    }
}


int main()
{
    ios :: sync_with_stdio(false);
    fin.tie(NULL);

    int n,m,x,y,c;

    fin >> n >> m;

    for(int i = 1; i <= m; i++){
        fin >> x >> y >> c;

        G[x].push_back(y);
        G[y].push_back(x);
        F[x][y].f += c;
        F[y][x].f += c;
        F[x][y].p = i;
        F[y][x].p = i;

    }

    int mflux;
    ant[1] = 1;
    while(BFS(n)){

        for(auto const &it : G[n]){
            if(B[it] == 1 && F[it][n].f > 0){

                ant[n] = it;
                mflux = INF;

                for(int j = n; j != ant[j]; j = ant[j])
                    mflux = min(mflux,F[ant[j]][j].f);

                for(int j = n; j != ant[j]; j = ant[j]){

                    F[ant[j]][j].f -= mflux;
                    F[j][ant[j]].f -= mflux;

                }
            }
        }
    }

    vector < int > S;
    DFS(1);
    DFS(n);
    for(int i = 1; i <= n; i++)
        for(int j = i + 1; j <= n; j++)
            if(F[i][j].f == 0 && F[i][j].p != 0 && F[i][j].b == 0){
                S.push_back(F[i][j].p);
        }
  //  fout << S.size();
    for(auto const &it : S)
        fout << it << "\n";
    return 0;
}