Cod sursa(job #1795351)

Utilizator cella.florescuCella Florescu cella.florescu Data 2 noiembrie 2016 11:26:53
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e3 + 1;
const int MAXM = 1e4 + 1;
const int INF = 2e9;

int flow[MAXN][MAXN], cap[MAXN][MAXN], father[MAXN], seen[MAXN];
vector <int> g[MAXN];
queue <int> q;

struct Edge {
  int x, y;
};

vector <Edge> e;

int bfs(int n) {
  memset(seen, 0, sizeof(seen));
  int node;
  seen[1] = 1;
  q.push(1);
  while (q.empty() == 0) {
    node = q.front();
    if (node != n)
      for (auto it : g[node])
        if (seen[it] == 0 && flow[node][it] < cap[node][it]) {
          seen[it] = 1;
          q.push(it);
          father[it] = node;
        }
    q.pop();
  }
  return seen[n];
}

inline int minim(int A, int B) {
  if (A < B)
    return A;
  return B;
}

void maxflow(int n) {
  int fl, node;
  while (bfs(n))
    for (auto it : g[n])
      if (seen[it] && flow[it][n] < cap[it][n]) {
        father[n] = it; fl = INF;
        for (node = n; node > 1; node = father[node])
          fl = minim(fl, cap[father[node]][node] - flow[father[node]][node]);
        for (node = n; node > 1; node = father[node]) {
          flow[father[node]][node] += fl;
          flow[node][father[node]] -= fl;
        }
      }
}

void dfs(int node, int marc) {
  seen[node] = marc;
  for (auto it : g[node])
    if (seen[it] == 0 && flow[node][it] < cap[node][it] && flow[it][node] < cap[it][node])
      dfs(it, marc);
}

vector <int> ans;

int main()
{
    int n, m, a, b, c, i;
    ifstream fin("critice.in");
    fin >> n >> m;
    for (i = 0; i < m; ++i) {
      fin >> a >> b >> c;
      e.push_back({a, b});
      cap[a][b] = cap[b][a] = c;
      g[a].push_back(b);
      g[b].push_back(a);
    }
    fin.close();
    maxflow(n);
    memset(seen, 0, sizeof(seen));
    dfs(1, 1);
    dfs(n, 2);
    for (i = 0; i < m; ++i)
      if (seen[e[i].x] * seen[e[i].y] && seen[e[i].x] != seen[e[i].y])
        ans.push_back(i + 1);
    ofstream fout("critice.out");
    fout << ans.size() << '\n';
    for (auto it : ans)
      fout << it << '\n';
    fout.close();
    return 0;
}