Cod sursa(job #2900216)

Utilizator raulandreipopRaul-Andrei Pop raulandreipop Data 10 mai 2022 15:23:16
Problema Critice Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.45 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 viz1[1001];
bool vizn[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<int> q;
    q.push(1);
    viz1[1] = 1;
    while (!q.empty())
    {
        int nod = q.front();
        q.pop();
        viz1[nod] = 1;

        for (auto to : adj[nod])
        {
            if (!viz1[to] && flux[nod][to].s - flux[nod][to].f > 0)
            {
             //   par[to] = crt;
                q.push(to);
            }
        }
    }

    q.push(n);
    vizn[n] = 1;
    while (!q.empty())
    {
        int nod = q.front();
        q.pop();
        vizn[nod] = 1;

        for (auto to : adj[nod])
        {
            if (!vizn[to] && flux[to][nod].s - flux[to][nod].f > 0)
            {
             //   par[to] = crt;
                q.push(to);
            }
        }
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (i != j)
            {
                if (nredge[i][j] && viz1[i] == 1 && viz1[j] == 0
                    && vizn[i] == 0 && vizn[j] == 1)
                    {
                        ans.push_back(nredge[i][j]);
                    }
            }
        }
    }

    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;

}