Cod sursa(job #1913656)

Utilizator geo_furduifurdui geo geo_furdui Data 8 martie 2017 13:30:37
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include <fstream>
#include<climits>
using namespace std;
ifstream cin("maxflow.in");
ofstream cout ("maxflow.out");
int cost[1010][1010];
int start[1010];
int n,m,nr,flux;
int used[1010];
int coada[1010];
int tata[1010];
struct bla
{
      int nod,urm;
} t[10010];
void read ()
{ int a,b,c,k=0;
      cin>>n>>m;
      for(int i=1;i<=m;i++)
      {
            cin>>a>>b>>c;
            cost[a][b]=c;
            t[++k].nod=b; t[k].urm=start[a]; start[a]=k;
            t[++k].nod=a; t[k].urm=start[b]; start[b]=k;
      }
}
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)
                  {
                        used[t[x].nod]=nr;
                        tata[t[x].nod]=coada[ps];
                        coada[++pi]=t[x].nod;
                  }
            } ++ps;
      }
      if(used[n]<nr) return false;
      return true;
}
void fluxx ()
{ nr++;
      while(bfs())
      {
          for(int x=start[n];x;x=t[x].urm)
          {
                if( used[t[x].nod]==nr && cost[t[x].nod][n]>0 )
                {
                      tata[n]=t[x].nod;
                      int maxim=INT_MAX;
                      for(int y=n;y!=1;y=tata[y]) maxim=min(maxim,cost[tata[y]][y]);
                      for(int y=n;y!=1;y=tata[y])
                      {
                            cost[tata[y]][y]-=maxim;
                            cost[y][tata[y]]+=maxim;
                      }
                      flux+=maxim;
                }
          } ++nr;
      }
}
void write ()
{
      cout<<flux;
}
int main()
{
    read();
    fluxx();
    write();
    cin.close();
    cout.close();
    return 0;
}