Cod sursa(job #2286230)

Utilizator ApostolIlieDanielApostol Daniel ApostolIlieDaniel Data 19 noiembrie 2018 22:45:39
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.26 kb
#include <bits/stdc++.h>

using namespace std;

FILE *fin = fopen ("critice.in", "r"), *fout = fopen ("critice.out", "w");

const int MAXN = 1000, INF = 1e9;

int dist[MAXN + 1], q[MAXN + 1], nxt[MAXN + 1], c[MAXN + 1][MAXN + 1];
vector <int> gr[MAXN + 1];

const int MAXM = 1e4;

int way[MAXN + 1];
pair <int, int> ad[MAXM + 1];
int v[MAXM + 1];

int n;

int bfs () {
  int e, b, nod;
  memset (dist, 0, sizeof (dist));
  e = 0;
  b = 0;
  q[e] = 1;
  nxt[1] = 0;
  dist[1] = 1;
  while (e <= b) {
    nod = q[e++];
    for (auto fiu : gr[nod]) {
      if (c[nod][fiu] > 0 && dist[fiu] == 0) {
        q[++b] = fiu;
        dist[fiu] = 1;
        nxt[fiu] = nod;
      }
    }
  }
  return dist[n];
}

int update () {
  int nod, nr, flow;
  nod = n;
  nr = 0;
  flow = INF;
  while (nod != 1) {
    nr++;
    flow = min (flow, c[nxt[nod]][nod]);
    if (nr > n * 5)
      return 0;
    nod = nxt[nod];
  }
  nod = n;
  while (nod != 1) {
    c[nxt[nod]][nod] -= flow;
    c[nod][nxt[nod]] += flow;
    nod = nxt[nod];
  }
  return flow;
}

void bfs_calc (int sursa, int ow) {
  int e, b, nod;
  e = 0;
  b = 0;
  q[e] = sursa;
  way[sursa] = ow;
  while (e <= b) {
    nod = q[e++];
    for (auto fiu : gr[nod]) {
      if (c[nod][fiu] > 0 && way[fiu] == 0) {
        q[++b] = fiu;
        way[fiu] = ow;
      }
    }
  }
}

int main() {
  int m, x, y, cap, sol, i;
  fscanf (fin, "%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    fscanf (fin, "%d%d%d", &x, &y, &cap);
    ad[i] = {x, y};
    c[x][y] = c[x][y] + cap;
    c[y][x] = c[y][x] + cap;
    gr[x].push_back (y);
    gr[y].push_back (x);
  }
  while (bfs ()) {
    for (auto fiu : gr[n]) {
      if (c[n][fiu] > 0) {
        nxt[n] = fiu;
        int _ = update ();
      }
    }
  }
  bfs_calc (1, 1);
  bfs_calc (n, 2);
  sol = 0;
  //fprintf (fout, "%d %d    %d %d\n", way[ad[1].first][0], way[ad[1].second][0], way[ad[1].first][1], way[ad[1].second][1]);
  for (i = 1; i <= m; i++)
    if (way[ad[i].first] * way[ad[i].second] && way[ad[i].second] != way[ad[i].first]) {
      v[++sol] = i;
    }
  fprintf (fout, "%d\n", sol);
  for (i = 1; i <= sol; i++)
    fprintf (fout, "%d\n", v[i]);
  fclose (fin);
  fclose (fout);
  return 0;
}