Cod sursa(job #2095528)

Utilizator amaliarebAmalia Rebegea amaliareb Data 27 decembrie 2017 17:23:15
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>

using namespace std;
ifstream f("critice.in");
ofstream g("critice.out");
const int MaxN = 1005;
int cap[MaxN][MaxN], flow[MaxN][MaxN], n, m, from[MaxN], vans[10 * MaxN], a[MaxN];
bool viz[MaxN];
pair<int, int> mm[10 * MaxN];
vector<int> gr[MaxN];

bool bfs()
{
    queue<int> q;
    memset(viz, 0, sizeof(viz));
    viz[1] = 1;
    q.push(1);
    while (!q.empty() && !viz[n])
    {
        int node = q.front();
        q.pop();
        for (auto son: gr[node])
        {
            if (!viz[son] && flow[node][son] < cap[node][son])
            {
                from[son] = node;
                viz[son] = 1;
                q.push(son);
            }
        }
    }
    return viz[n];
}

void dfs(int node, int val)
{
    a[node] = val;
    for (auto son: gr[node]) if (!a[son] && flow[node][son] < cap[node][son] && flow[son][node] < cap[son][node]) dfs(son, val);
}

int main()
{
    f >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        int x, y, c;
        f >> x >> y >> c;
        gr[x].push_back(y);
        gr[y].push_back(x);
        cap[x][y] = cap[y][x] = c;
        mm[i] = {x, y};
    }
    while (bfs())
    {
        for (int i = 1; i < n; ++i)
        {
            if (viz[i] && flow[i][n] < cap[i][n])
            {
                from[n] = i;
                int node = n, mini = 1000000000;
                while (from[node])
                {
                    mini = min(mini, cap[from[node]][node] - flow[from[node]][node]);
                    node = from[node];
                }
                node = n;
                while (from[node])
                {
                    flow[from[node]][node] += mini;
                    flow[node][from[node]] -= mini;
                    node = from[node];
                }
            }
        }
    }
    int ans = 0;
    dfs(1, 1);
    dfs(n, 2);
    for (int i = 1; i <= m; ++i)
    {
        int x = mm[i].first, y = mm[i].second;
        if ((a[x] == 1 && a[y] == 2) || (a[x] == 2 && a[y] == 1)) vans[++ans] = i;
    }
    g << ans << '\n';
    for (int i = 1; i <= ans; ++i) g << vans[i] << '\n';
    return 0;
}