Cod sursa(job #3193560)

Utilizator Radu_MocanasuMocanasu Radu Radu_Mocanasu Data 14 ianuarie 2024 22:02:20
Problema Critice Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.07 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];
queue < pair <int, int> > q;
int t[1002];
bitset <1002> uz;
int v1[1002], v2[1002];
int bfs(){
    while(!q.empty()) q.pop();
    memset(t, -1, sizeof t);
    q.push({1,1e9});
    uz.reset();
    uz[1] = 1;
    while(!q.empty()){
        int f = q.front().first;
        for(auto x : G[q.front().first]){
            if(!uz[x] && cap[q.front().first][x] > 0){
                uz[x] = 1;
                t[x] = q.front().first;
                int new_flow = min(q.front().second, cap[t[x]][x]);
                if(x == n) return new_flow;
                q.push({x, new_flow});
            }
        }
        q.pop();
    }
    return 0;
}
queue <int> q2;
void bfs2(int start, int v[]){
    while(!q2.empty()) q2.pop();
    q2.push(start);
    v[start] = 1;
    while(!q2.empty()){
        for(auto x : G[q2.front()]){
            if(!v[x] && cap[q2.front()][x] > 0){
                v[x] = 1;
                q2.push(x);
            }
        }
        q2.pop();
    }
}
void edmonds_karp(){
    int flow = 0, new_flow;
    while(new_flow = bfs()){
        flow += new_flow;
        int e = n;
        while(t[e] != -1){
            cap[t[e]][e] -= new_flow;
            cap[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();
    bfs2(1, v1);
    bfs2(2, v2);
    for(i = 0; i < edges.size(); i++){
        auto x = edges[i];
        if((v1[x.first] & v2[x.second]) || (v2[x.first] & v1[x.second])){
            sol.push_back(i + 1);
            t++;
        }
    }
    fout << t << "\n";
    for(auto x : sol) fout << x << "\n";
    return 0;
}