Cod sursa(job #3204019)

Utilizator csamoilasamoila ciprian casian csamoila Data 15 februarie 2024 14:13:10
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.62 kb
#include <bits/stdc++.h>
#define NMAX 1005
#define INF 1e4 + 1

using namespace std;

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

int n, m;
vector<int> adj[NMAX];
int cap[NMAX][NMAX];
int flux[NMAX][NMAX];
vector<pair<int, int>> edges;

int bfs(vector<int>& p) {
    fill(p.begin(), p.end(), -1);
    p[0] = -2;
    queue<pair<int, int>> q;
    q.push({1, INF});

    while (!q.empty()) {
        int cur = q.front().first;
        int flow = q.front().second;
        q.pop();

        for (int next : adj[cur]) {
             if(p[next] == -1 && cap[cur][next] > flux[cur][next]) {
                p[next] = cur;
                int new_flow = min(flow, cap[cur][next] - flux[cur][next]);
                if(next == n)
                    return new_flow;
                q.push({next, new_flow});
            }
        }
    }
    return 0;
}

int maxflow() {
    vector<int> p (n + 1);
    int flow;
    int max_flow = 0;

    while ((flow = bfs(p))) {
        int cur = n;
        while(cur != 1) {
            int prev = p[cur];
            flux[prev][cur] += flow;
            flux[cur][prev] -= flow;
            cur = prev;
        }
        max_flow += flow;
    }
    return max_flow;
}

map<int, int> f;
vector<int> viz;

int code(int a, int b) {
    int aa = min(a, b);
    int bb = max(a, b);
    return aa * 1001 + bb;
}

void dfs(int cur, int sgn) {
    //cout << cur << '\n';
    for(auto next : adj[cur])
        if(viz[next] == 0) {
            if(cap[cur][next] > sgn*flux[cur][next]) {
                viz[next] = 1;
                dfs(next, sgn);
            }else{
                f[code(cur, next)] ++;
            }
        }
    return;
}

map<int, int> poz;

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int a, b, c;
        fin >> a >> b >> c;
        adj[a].push_back(b);
        adj[b].push_back(a);
        cap[a][b] = c;
        cap[b][a] = c;
        poz[code(a, b)] = i;
    }
    maxflow();
//    for(int i = 1; i <= n; i++) {
//        for(int j = 1; j <= n; j++)
//            cout << flux[i][j] << ' ';
//        cout << '\n';
//    }
    viz = vector<int> (n + 1);
    viz[1] = 1;
    dfs(1, 1);

    viz = vector<int> (n + 1, 0);
    viz[n] = 1;
    dfs(n, -1);
//    for(auto it : f)
//        cout << it.first << ' ' << it.second << '\n';
    int k = 0;
    set<int> s;
    for(auto i : f){
        if(i.second == 2) {
            k++;
            s.insert(poz[i.first]);
        }
    }
    fout << k << '\n';
    for(auto it : s)
        fout << it << '\n';
    return 0;
}