Cod sursa(job #2900200)

Utilizator raulandreipopRaul-Andrei Pop raulandreipop Data 10 mai 2022 15:12:42
Problema Critice Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3 kb
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define s second
#define f first

using namespace std;

typedef long long ll;

int n, m;
pair<ll , ll> flux[1005][1004];
vector<vector<int>> adj;
bool viz[1001];
int par[1001];
bool vizcrt[1001][1001];
int nredge[1001][1011];

bool bfs ()
{
    
    queue<int> q;
    q.push(1);
    viz[1] = true;
    bool found = 0;
    while (!q.empty())
    {
        int crt = q.front();
        
        q.pop();
        viz[crt] = true;
        if (crt == n){
            return 1;
        }
        else {
            for (auto to : adj[crt])
            {
                if (!viz[to] && flux[crt][to].s - flux[crt][to].f > 0)
                {
                    par[to] = crt;
                    q.push(to);
                }
            }
        }
    }
    return 0;
}

int main ()
{
    freopen("critice.in", "r", stdin);
    freopen("critice.out", "w", stdout);
    cin >> n >> m;
    adj.resize(n + 1);
    for (int i = 1; i <= m; i++)
    {
        int x, y; ll fx; cin >> x >> y >> fx;
        adj[x].push_back(y);
        adj[y].push_back(x);
        flux[x][y].s += fx;
        nredge[x][y] = i;
        nredge[y][x] = i;
    }
    ll bottleneck = 0;
//    ll ans = 0;

    while (bfs())
    {
        
        for (auto leaf : adj[n])
        {
            if (!viz[leaf] || !(flux[leaf][n].s - flux[leaf][n].f > 0))
                continue;
            par[n] = leaf;
            
            int v = n;
            ll minflow = 1e9;
            while (v != 1)
            {
                minflow = min(minflow, flux[par[v]][v].s - flux[par[v]][v].f);
                if (v == 0) continue;
                v = par[v];
            }
            v = n;
            while (v != 1)
            {
                flux[par[v]][v].f += minflow;
                flux[v][par[v]].f -= minflow;
                v = par[v];
            }
            
        }
        for (int i = 1; i <= n; i++)
        {
            par[i] = -1;
            viz[i] = 0;
        }
    }

    vector<int> ans;
    queue<pair<int, int>> q;
    q.push({1, 0});
    vizcrt[1][0] = 1;
    while (!q.empty()){
        pair<int , int> crt = q.front();
        q.pop();
        vizcrt[crt.f][crt.s] = 1;

        if (crt.f == n && crt.s != 0)
        {
            ans.push_back(crt.s);
        }
        for (auto to : adj[crt.f])
        {
            if (!vizcrt[to][crt.s] && flux[crt.f][to].s - flux[crt.f][to].f > 0)
            {
            //    par[to] = crt;
                q.push({to, crt.s});
            }
            else if (crt.s == 0 && !vizcrt[to][crt.f])
            {
                q.push({to, nredge[crt.f][to]});
            }
        }
    }

    sort(ans.begin(), ans.end());
    cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i] << '\n';
    //    while (ans[i] == ans[i+1]) i++;
    }
    return 0;

}