Cod sursa(job #1567261)

Utilizator serbanSlincu Serban serban Data 13 ianuarie 2016 00:32:44
Problema Critice Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.33 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

int oo = 1 << 30, n;
vector<int> V[1005];
int c[1005][1005];
int prec[1005];

vector<int> aux;
queue<int> Q;

void bfs() {
    prec[1] = -1; Q.push(1);
    while(!Q.empty()) {
        int top = Q.front(); Q.pop();
        for(int i = 0; i < V[top].size(); i ++) {
            int cur = V[top][i];
            if(cur != n && c[top][cur] && !prec[cur]) {
                prec[cur] = top;
                if(c[cur][n]) aux.push_back(cur);
                Q.push(cur);
            }
        }
    }
}

vector< pair<int, int> > M;
bool verif(int i);

int main()
{
    M.push_back(make_pair(0, 0));
    ifstream d("critice.in");
    ofstream g("critice.out");
    int m, x, y, z; d >> n >> m;
    for(int i = 1; i <= m; i ++) {
        d >> x >> y >> z;
        M.push_back(make_pair(x, y));
        V[x].push_back(y);
        V[y].push_back(x);
        c[x][y] = c[y][x] = z;
    }

    while(true) {
        bfs();
        if(!aux.size()) break;
        for(int i = 0; i < aux.size(); i ++) {
            int j = aux[i];
            int mn = c[j][n];
            for(int l = j; prec[l] != -1; l = prec[l])
                mn = min(mn, c[prec[l]][l]);

            c[j][n] -= mn; c[n][j] -= mn;
            for(int l = j; prec[l] != -1; l = prec[l])
                c[prec[l]][l] -= mn, c[l][prec[l]] -= mn;
        }
        for(int i = 1; i <= n; i ++) prec[i] = 0;
        while(!aux.empty()) aux.pop_back();
    }

    for(int i = 1; i <= m; i ++)
        if(!c[ M[i].first ][ M[i].second ] && verif(i)) aux.push_back(i);
    g << aux.size() << "\n";
    for(int i = 0; i < aux.size(); i ++) g << aux[i] << "\n";
    return 0;
}

void DFS(int node);

bool viz[1005];
bool verif(int i) {
    bool ok1 = false, ok2 = false;
    for(int j = 1; j <= n; j ++) viz[j] = false;
    DFS(M[i].first);
    ok1 = viz[1];
    ok2 = viz[n];

    for(int j = 1; j <= n; j ++) viz[j] = false;
    DFS(M[i].second);
    ok1 |= viz[1];
    ok2 |= viz[n];
    if(ok1 && ok2) return true;
    return false;
}

void DFS(int node) {
    viz[node] = true;
    for(int i = 0; i < V[node].size(); i ++) {
        int now = V[node][i];
        if(!viz[now] && c[now][node] > 0) DFS(now);
    }
}