Cod sursa(job #1941272)

Utilizator oanaroscaOana Rosca oanarosca Data 27 martie 2017 09:22:44
Problema Flux maxim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
#include <queue>

using namespace std;

int noduri, muchii, x, y, c, fmax, maxflow, i, m[1001][1001], t[1001];
bool viz[1001];
queue<int>q;

void bfs (int nod) {
  int nodc = 1;

  q.push(nod);
  while (!q.empty()) {
    nodc = q.front(); q.pop(); viz[nodc] = true;
    for (int i = 1; i <= noduri; i++)
      if (m[nodc][i] > 0 and !viz[i])
        q.push(i), t[i] = nodc;
  }
}

int main () {
  ifstream fi("maxflow.in");
  ofstream fo("maxflow.out");
  fi >> noduri >> muchii;
  for (int i = 1; i <= muchii; i++)
    fi >> x >> y >> c, m[x][y] = c;
  while (true) {
    for (int i = 1; i <= noduri; i++)
      viz[i] = false;
    bfs(1);
    if (!viz[noduri])
      break;
    fmax = 2e9, i = noduri;
    while (i != 1)
      fmax = min(fmax, m[t[i]][i]), i = t[i];
    i = noduri;
    while (i != 1)
      m[t[i]][i] -= fmax, m[i][t[i]] += fmax, i = t[i];
    maxflow += fmax;
  }
  fo << maxflow;
  return 0;
}