Cod sursa(job #3274112)

Utilizator unomMirel Costel unom Data 5 februarie 2025 09:00:12
Problema Critice Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream in("critice.in");
ofstream out("critice.out");
int n, m, ok, ans;
vector<pair<int, int>> v[1005];
pair<int, int> muc[20005];
pair<int, int> viz[1005];
int viz2[1005];
int INF = (1 << 30);

void bfs(int cmin)
{
    viz[1] = {-1, -1};
    queue<int> q;
    q.push(1);

    while(!q.empty())
    {
        int nod = q.front();
        q.pop();

        if(nod == n)
        {
            break;
        }

        for(auto it: v[nod])
        {
            if(viz[it.first].first == 0 && muc[it.second].second - muc[it.second].first >= cmin)
            {
                viz[it.first] = {nod, it.second};
                q.push(it.first);
            }
        }
    }

    if(viz[n].first == 0)
    {
        return;
    }

    ok = 1;
    int nod = n;
    int flux = INF;

    while(viz[nod].first != -1)
    {
        flux = min(flux, muc[viz[nod].second].second - muc[viz[nod].second].first);

        nod = viz[nod].first;
    }

    nod = n;
    while(viz[nod].first != -1)
    {
        if(viz[nod].second <= m)
        {
            muc[viz[nod].second].first += flux;
            muc[viz[nod].second + m].first -= flux;
        }
        else
        {
            muc[viz[nod].second].first += flux;
            muc[viz[nod].second - m].first -= flux;
        }

        nod = viz[nod].first;
    }

}

void dfs2(int nod)
{
    viz2[nod] = 1;

    for(auto it: v[nod])
    {
        if(viz2[it.first] == 0 && muc[it.second].second - muc[it.second].first > 0)
        {
            dfs2(it.first);
        }
    }
}

int main()
{
    in>>n>>m;

    int x, y, z;
    int cmax = 0;
    for(int i = 1; i<=m; i++)
    {
        in>>x>>y>>z;

        v[x].push_back({y, i});
        muc[i] = {0, z};

        v[y].push_back({x, i + m});
        muc[i + m] = {0, z};

        cmax += 2 * z;
    }

    for(int i = cmax; i>=1; i /= 2)
    {
        ok = 1;
        while(ok == 1)
        {
            ok = 0;

            for(int j = 1; j<=n; j++)
            {
                viz[j] = {0, 0};
            }

            bfs(i);
        }
    }

    dfs2(1);

    for(int i = 1; i<=n; i++)
    {
        if(viz2[i] == 1)
        {
            for(auto it: v[i])
            {
                if(viz2[it.first] == 0)
                {
                    ans++;
                }
            }
        }
    }

    out<<ans<<'\n';
    for(int i = 1; i<=n; i++)
    {
        if(viz2[i] == 1)
        {
            for(auto it: v[i])
            {
                if(viz2[it.first] == 0)
                {
                    if(it.second <= m)
                    {
                        out<<it.second<<'\n';
                    }
                    else
                    {
                        out<<it.second - m<<'\n';
                    }
                }
            }
        }
    }

    return 0;
}