Cod sursa(job #1941281)

Utilizator oanaroscaOana Rosca oanarosca Data 27 martie 2017 09:39:19
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>

using namespace std;

bool viz[1001];
int n, muchii, x, y, c, maxflow, m[1001][1001], t[1001], fmax, q[5001];

void bfs (int nod) {
   int p, i, u;

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