Pagini recente » Cod sursa (job #1580549) | Monitorul de evaluare | Cod sursa (job #1029414) | Cod sursa (job #2429468) | Cod sursa (job #2029124)
//
// L.cpp
//
// Created by Vlad Turcuman on 24/09/2017.
// Copyright © 2017 Vlad Turcuman. All rights reserved.
//
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#define pii pair<int,int>
#define fs first
#define sc second
#define NMax 1010
#define oo 2000000000
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
struct queElement{
int a,b;
int flow,ant;
}tmp,tmp2;
int No,n,maxFlow;
vector<int> edges[NMax];
vector<queElement> Que;
int vis[NMax];
pii mat[NMax][NMax];
int bfs()
{
int ok=0;
No ++;
vis[1] = No;
Que.clear();
tmp.a = -1;
tmp.b = 1;
tmp.ant = -1;
tmp.flow = oo;
Que.push_back(tmp);
int iterator = 0;
while(iterator<Que.size())
{
tmp = Que[iterator++];
for(int i=0;i<edges[tmp.b].size();i++)
{
if(vis[edges[tmp.b][i]] != No)
{
tmp2.a = tmp.b;
tmp2.b = edges[tmp.b][i];
tmp2.ant = iterator-1;
tmp2.flow = min(tmp.flow, mat[tmp2.a][tmp2.b].sc - mat[tmp2.a][tmp2.b].fs);
if(tmp2.flow > 0)
{
vis[edges[tmp.b][i]] = No;
Que.push_back(tmp2);
if(tmp2.b == n)
ok=1;
}
}
}
}
return ok;
}
void FindMaxFlow()
{
maxFlow = 0;
int flow;
while(bfs())
{
No++;
int node;
for(int i=Que.size()-1;i>0;i--)
{
if(vis[i]!= No && Que[i].b == n)
{
/// Find the flow
int iterator = i;
int flow = Que[i].flow;
while(iterator > 0)
{
int antNode = Que[iterator].a;
int curNode = Que[iterator].b;
flow = min(flow,mat[antNode][curNode].sc - mat[antNode][curNode].fs);
if(!flow)
continue;
iterator = Que[iterator].ant;
}
/// Find the path
iterator = i;
while(iterator > 0)
{
int antNode = Que[iterator].a;
int curNode = Que[iterator].b;
mat[antNode][curNode].fs += flow;
mat[curNode][antNode].fs -= flow;
iterator = Que[iterator].ant;
}
maxFlow += flow;
}
}
}
}
int main()
{
int m,a,b,c;
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>a>>b>>c;
mat[a][b].sc = c;
edges[a].push_back(b);
edges[b].push_back(a);
}
FindMaxFlow();
fout<<maxFlow;
return 0;
}