Cod sursa(job #963257)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 16 iunie 2013 22:34:04
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");

#define inf 1<<30
#define LE 1066

int que[LE+666],t,k,father[LE];
int fl[LE][LE],mxl[LE],n,m;
bool viz[LE];
int i,xx,yy,cap,flux;

void bfs()
{
   for(int i=1;i<=n;++i) viz[i]=false;
   que[(k=1)]=1,viz[1]=true;
   t=0;

   for(int i=1;i<=k;++i){
       for(int j=1;j<n;++j)
         if (viz[j]==false&&fl[que[i]][j]>0){
           viz[j]=true;
           que[++k]=j;
           father[j]=que[i];
         }
      if (fl[que[i]][n]>0) mxl[++t]=que[i];
   }
}

int main()
{
   f>>n>>m;

   for(i=1;i<=m;++i){
     f>>xx>>yy>>cap;
     fl[xx][yy]=cap;
   }

   while (true)
   {
       bfs();
       if (t==0) break;


       for(i=1;i<=t;++i){
         int fmax=inf,node;
         father[n]=mxl[i],node=n;


         while (node!=1){
           fmax=min(fmax,fl[father[node]][node]);
           node=father[node];
         }

         node=n;
         if(fmax==0) continue;

         while (node!=1){
            fl[father[node]][node]-=fmax;
            fl[node][father[node]]+=fmax;
            node=father[node];
         }
         flux+=fmax;
       }
   }

  g<<flux<<'\n';

  f.close();
  g.close();
  return 0;
}