Cod sursa(job #2660840)

Utilizator LeVladzCiuperceanu Vlad LeVladz Data 20 octombrie 2020 17:24:34
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.65 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("critice.in");
ofstream fout("critice.out");

int n,m,i,x,y,z,resid[1005][1005],tata[1005],sol[1005];
pair<int, int> v[10005],d[1005];
vector<int> L[1005];
deque<int> c;
bitset<1005> f;

int bfs()
{
    f.reset(); c.push_back(1); f[1] = 1; tata[1] = -1;
    while (!c.empty())
    {
        int nod = c.front(); c.pop_front();
        for (int i=0; i<L[nod].size(); i++)
        {
            int vecin = L[nod][i];
            if (!f[vecin] && resid[nod][vecin] > 0)
            {
                c.push_back(vecin);
                f[vecin] = 1; tata[vecin] = nod;
            }
        }
    }
    return f[n];
}

void bfs_verif(int start, int val)
{
    f.reset(); c.push_back(start); f[start] = 1;
    while (!c.empty())
    {
        int nod = c.front(); c.pop_front();
        if (start == 1)
            d[nod].first = 1;
        else
            d[nod].second = 1;
        for (int i=0; i<L[nod].size(); i++)
        {
            int vecin = L[nod][i];
            if (!f[vecin] && resid[nod][vecin] > 0 && resid[vecin][nod] > 0)
            {
                c.push_back(vecin);
                f[vecin] = 1;
            }
        }
    }
}

int main()
{
    fin >> n >> m;
    for (i=1; i<=m; i++)
    {
        fin >> x >> y >> z;
        L[x].push_back(y); L[y].push_back(x);
        resid[x][y] = resid[y][x] = z;
        v[i] = {x, y};
    }
    int flow = 0;
    while (bfs())
        for (i=0; i<L[n].size(); i++)
        {
            int vecin = L[n][i];
            if (f[vecin] && resid[vecin][n] > 0)
            {
                int path_flow = resid[vecin][n];
                while (vecin != 1)
                {
                    path_flow = min(path_flow, resid[tata[vecin]][vecin]);
                    vecin = tata[vecin];
                }
                vecin = L[n][i];
                resid[vecin][n] -= path_flow;
                resid[n][vecin] += path_flow;
                while (vecin != 1)
                {
                    resid[tata[vecin]][vecin] -= path_flow;
                    resid[vecin][tata[vecin]] += path_flow;
                    vecin = tata[vecin];
                }
                flow += path_flow;
            }
        }
    bfs_verif(1, 1); bfs_verif(n, 2); int k = 0;
    for (i=1; i<=m; i++)
    {
        x = v[i].first; y = v[i].second;
        if ((resid[x][y] == 0 && d[x].first && d[y].second) || (resid[y][x] == 0 && d[x].second && d[y].first))
            sol[++k] = i;
    }
    fout << k << "\n";
    for (i=1; i<=k; i++)
        fout << sol[i] << "\n";
    return 0;
}