Cod sursa(job #2565619)

Utilizator nicolaee2Martinescu Nicolae nicolaee2 Data 2 martie 2020 15:31:38
Problema Flux maxim Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include<bits/stdc++.h>
#include<string.h>

using namespace std;

#define NMAX 1005
#define INF (1<<30)-1

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

int n,m,s;
vector<int> ma[NMAX];
int cap[NMAX][NMAX];

void citire()
{
   fin>>n>>m;
   for(int i=1;i<=m;i++)
   {
      int x,y,c;
      fin>>x>>y>>c;
      cap[x][y] += c;
      cap[y][x] = 0;
      ma[x].push_back(y);
      ma[y].push_back(x);
   }

}


int bfs(int s,int t,vector<int>& parent)
{
   fill(parent.begin(),parent.end(),-1);
   parent[s] = -2;
   queue<pair<int,int> > q;
   q.push({s,INF});

   while(!q.empty())
   {
      int curent = q.front().first;
      int flow = q.front().second;
      q.pop();

      for(int i=0;i<ma[curent].size();i++)
      {
         int vecin = ma[curent][i];
         if(parent[vecin] == -1 && cap[curent][vecin])
         {
            parent[vecin] = curent;
            int new_flow = min(flow,cap[curent][vecin]);
            if(vecin == t)
               return new_flow;
            else
            q.push({vecin,new_flow});
         }

      }


   }

   return 0;
}

int rezolva(int s,int t)
{
   int flux = 0;
   int new_flow = 0;

   vector<int> parent(n+2);

   while(new_flow = bfs(s,t,parent))
   {
      int cur = t;
      flux += new_flow;
      while(cur != s)
      {
         int prev = parent[cur];



         cap[prev][cur] -= new_flow;
         cap[cur][prev] += new_flow;

         cur = prev;

      }

   }


   return flux;

}

int main()
{

   citire();
   fout<<rezolva(1,n);

}