Cod sursa(job #1806929)

Utilizator BrandonChris Luntraru Brandon Data 15 noiembrie 2016 20:44:59
Problema Critice Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.52 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

#define Pe pair <int, int>
#define mp make_pair
#define fi first
#define se second
using namespace std;

ifstream cin ("critice.in");
ofstream cout ("critice.out");

const int MaxN = 1005, Inf = 0x3f3f3f3f;

vector <Pe> Edg;
vector <int> Ans, G[MaxN];
queue <int> Q;
int n, m;
int Capacity[MaxN][MaxN], Flow[MaxN][MaxN], father[MaxN];
int used[MaxN];

inline void ClearQ() {
  while (Q.size()) {
    Q.pop();
  }
}

int Bfs(int StNode = 1) {
  ClearQ();
  memset(father, 0, sizeof father);
  Q.push(StNode);
  father[StNode] = -1;
  while (Q.size()) {
    int node = Q.front();
    Q.pop();
    if (node == n) {
      continue;
    }

    for (auto i: G[node]) {
      if (!father[i] and Capacity[node][i] - Flow[node][i]) {
        Q.push(i);
        father[i] = node;
      }
    }
  }

  return father[n];
}

inline int CalcUpdate(int node = n) {
  int ans = Inf;
  while (father[node] != -1) {
    int parent = father[node];
    ans = min(ans, Capacity[parent][node] - Flow[parent][node]);
    node = parent;
  }

  return ans;
}

inline void FlowUpdate(int Quantity, int node = n) {
  while (father[node] != -1) {
    int parent = father[node];
    Flow[parent][node] += Quantity;
    Flow[node][parent] -= Quantity;
    node = parent;
  }
}

inline int FlowAllowed(int type, int node, int nxt) {
  if (type == 1) {
    return Capacity[node][nxt] - Flow[node][nxt];
  }

  return Capacity[nxt][node] - Flow[nxt][node];
}

void Dfs(int node, int type) {
  used[node] = type;
  for (auto nxt: G[node]) {
    if (used[nxt] or !FlowAllowed(type, node, nxt)) {
      continue;
    }

    Dfs(nxt, type);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(b);
    G[b].push_back(a);
    Capacity[a][b] = c;
    Capacity[b][a] = c;
    Edg.push_back(mp(a, b));
  }

  while (Bfs()) {
    for (auto parent: G[n]) {
      if (!(Capacity[parent][n] - Flow[parent][n]) or !father[parent]) {
        continue;
      }

      int Quantity = CalcUpdate();
      FlowUpdate(Quantity);
    }
  }

  Dfs(1, 1);
  Dfs(n, 2);
  for (int i = 0; i < m; ++i) {
    int node1 = Edg[i].fi;
    int node2 = Edg[i].se;
    if (used[node1] and used[node2] and used[node1] != used[node2]) {
      Ans.push_back(i + 1);
    }
  }

  cout << Ans.size() << '\n';
  for (auto it: Ans) {
    cout << it << '\n';
  }
  return 0;
}