Cod sursa(job #2695326)

Utilizator Johnny07Savu Ioan-Daniel Johnny07 Data 12 ianuarie 2021 14:58:53
Problema Critice Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.48 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");

int n,m;
vector<int> firstNode, secondNode;
vector<int> edges[1001];
int graph[1001][1001];
int parent[1005];

bool visited[1005];

int bfs()
{
    int s = 1;
    int t = n;
    memset(parent, 0, sizeof(parent));

    queue <int> q;
    q.push(s);
    parent[s] = -1;

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

        if (node != n)
        for (auto nextNode : edges[node])
        {
            if (!parent[nextNode] && graph[node][nextNode] > 0)
            {
                q.push(nextNode);
                parent[nextNode] = node;

            }
        }


    }
    return parent[t];

}


void dfs (int s)
{
    visited[s] = true;
    for (auto& nextNode : edges[s])
        if (graph[s][nextNode] && !visited[nextNode])
            dfs(nextNode);
}


void solve ()
{

    int node, path_flow;

    while (bfs())
    {
        for (auto& nextNode : edges[n])
            if (parent[nextNode] && graph[nextNode][n])
            {
                node = n;
                parent[n] = nextNode;
                while (node != 1)
                {
                    path_flow = min(path_flow, graph[parent[node]][node]);
                    node = parent[node];
                }
                node = n;
                if (path_flow > 0)
                    while (node != 1)
                    {
                        graph[parent[node]][node] -= path_flow;
                        graph[node][parent[node]] += path_flow;
                        node = parent[node];
                    }
            }
    }

    dfs(1);
    int ans = 0;
    for (int i = 1; i <= m; i++)
        if (visited[firstNode[i]] != visited[secondNode[i]])
            ans ++;

    fout << ans << "\n";

    for (int i = 1; i<= m; i++)
        if (visited[firstNode[i]] != visited[secondNode[i]])
            fout << i << "\n";


}



int main()
{
    fin >> n >> m;
    firstNode.resize(m + 5);
    secondNode.resize(m + 5);
    for(int i = 1; i <= m; ++i)
    {
        int c;
        fin >> firstNode[i] >> secondNode[i] >> c;

        edges[firstNode[i]].push_back(secondNode[i]);
        edges[secondNode[i]].push_back(firstNode[i]);

        graph[firstNode[i]][secondNode[i]] = c;
        graph[secondNode[i]][firstNode[i]] = c;
    }


    solve();





    return 0;
}