Cod sursa(job #3193586)

Utilizator Radu_MocanasuMocanasu Radu Radu_Mocanasu Data 14 ianuarie 2024 22:52:28
Problema Critice Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
int n;
vector <int> G[1002];
int cap[1002][1002], f[1002][1002];
queue <int> q;
int t[1002];
bitset <1002> uz;
bool v1[1002], v2[1002];
int bfs(){
    while(!q.empty()) q.pop();
    memset(t, -1, sizeof t);
    q.push(1);
    uz.reset();
    uz[1] = 1;
    while(!q.empty()){
        for(auto x : G[q.front()]){
            if(!uz[x] && cap[q.front()][x] > f[q.front()][x]){
                uz[x] = 1;
                t[x] = q.front();
                q.push(x);
            }
        }
        q.pop();
    }
    return uz[n];
}
void bfs3(int start, bool v[]){
    while(!q.empty()) q.pop();
    q.push(start);
    v[start] = 1;
    while(!q.empty()){
        for(auto x : G[q.front()]){
            if(!v[x] && -f[q.front()][x] < cap[q.front()][x]){
                v[x] = 1;
                q.push(x);
            }
        }
        q.pop();
    }
}
void edmonds_karp(){
    while(bfs()){
        for(auto x : G[n]){
            if(!uz[x] || cap[t[x]][x] <= f[t[x]][x]) continue;
            int e = n, new_flow = 1e9;
            t[n] = x;
            while(t[e] != -1){
                new_flow = min(new_flow, cap[t[e]][e] - f[t[e]][e]);
                e = t[e];
            }
            e = n;
            if(!new_flow) continue;
            while(t[e] != -1){
                f[t[e]][e] += new_flow;
                f[e][t[e]] -= new_flow;
                e = t[e];
            }
        }
    }
}
vector < pair <int, int> > edges;
vector <int> sol;
int main()
{
    int i,m,u,v,c,t = 0;
    fin >> n >> m;
    for(i = 1; i <= m; i++){
        fin >> u >> v >> c;
        G[u].push_back(v);
        G[v].push_back(u);
        cap[u][v] = cap[v][u] = c;
        edges.push_back({u,v});
    }
    edmonds_karp();
    bfs();
    bfs3(n, v2);
    for(i = 0; i < edges.size(); i++){
        auto x = edges[i];
        if((uz[x.first] & v2[x.second]) || (v2[x.first] & uz[x.second])){
            sol.push_back(i + 1);
            t++;
        }
    }
    fout << t << "\n";
    for(auto x : sol) fout << x << "\n";
    return 0;
}