Cod sursa(job #1897078)

Utilizator geo_furduifurdui geo geo_furdui Data 1 martie 2017 09:46:28
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <fstream>
#include<climits>
#include<algorithm>
using namespace std;
ifstream cin ("maxflow.in");
ofstream cout ("maxflow.out");
struct bla
{
  int nod, urm;
} t[5010];
int n, m, start[1010], p;
int cost[1010][1010];
int neighbor[1010];
int used[1010], nr;
int tata[1010], coada[1010];
int flux,ordine[1010];
void read ()
{
  int a, b, c, k = 0;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
  {
    cin >> a >> b >> c;
    if (b == n) neighbor[++p] = a;
    t[++k].nod = b;
    t[k].urm = start[a];
    start[a] = k;
 t[++k].nod = a;
    t[k].urm = start[b];
    start[b] = k;
    cost[a][b] = c;
  }
}
bool bfs ()
{
  int pi = 1, ps = 1;
  coada[1] = 1;
  used[1] = nr;
  while (ps <= pi)
  {
        for(int x=start[coada[ps]];x;x=t[x].urm)
        {
              if(used[t[x].nod]<nr && cost[coada[ps]][t[x].nod]>0)
              {
                  if(t[x].nod!=n)  coada[++pi]=t[x].nod; used[t[x].nod]=nr; tata[t[x].nod]=coada[ps];
              }
        } ++ps;
  }
  if (used[n] == nr) return true;
  return false;
}
void solve_here ()
{
  nr = 1;
  while (bfs() )
  {
    for (int i = 1; i <= p; i++)
    {
      int x = neighbor[i];
      if (used[x] == nr && cost[x][n] != 0)
      {
        int minim = INT_MAX;
        tata[n] = x;
        for (x = n; x != 1; x = tata[x]) minim = min (minim, cost[tata[x]][x]);
        for (x = n; x != 1; x = tata[x]) cost[tata[x]][x] -= minim, cost[x][tata[x]] += minim;
        flux += minim;
      }
    } ++nr;
  }
}
void write ()
{
  cout<<flux;
}
int main()
{
  read();
  solve_here();
  write();
  cin.close();
  cout.close();
  return 0;
}