Pagini recente » Cod sursa (job #1193313) | Cod sursa (job #1737044) | Cod sursa (job #2753712) | Cod sursa (job #846853) | Cod sursa (job #2029104)
//
// 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()
{
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)
return tmp2.flow;
}
}
}
}
return 0;
}
void FindMaxFlow()
{
maxFlow = 0;
int flow;
while(flow = bfs())
{
/// Find the path
int iterator = Que.size()-1;
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;
}