Pagini recente » Cod sursa (job #2064999) | Cod sursa (job #2812276) | Cod sursa (job #926251) | Cod sursa (job #318884) | Cod sursa (job #2452601)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
//-------------------------------------------
//Globale
int n,cap[1001][1001],flux[1001][1001],par[1001];
vector<vector<int>>v;
//-------------------------------------------
//Functii
void citire();
void rezolvare();
//-------------------------------------------
int main()
{
citire();
rezolvare();
return 0;
}
//-------------------------------------------
bool bfs()
{
for(int i = 1; i <= n; ++i)
par[i] = 0;
queue<int>q;
q.push(1);
while(!q.empty())
{
int nod = q.front();
if(nod == n)
return 1;
q.pop();
for(int i = 0; i < v[nod].size(); ++i)
if(cap[nod][v[nod][i]] > flux[nod][v[nod][i]] && par[v[nod][i]] == 0)
{
q.push(v[nod][i]);
par[v[nod][i]] = nod;
}
}
return 0;
}
//-------------------------------------------
void rezolvare()
{
int rasp = 0;
while(bfs())
{
int flow = 1000000;
int nod = n;
while(nod != 1)
{
flow = min(flow,cap[par[nod]][nod] - flux[par[nod]][nod]);
nod = par[nod];
}
nod = n;
while(nod != 1)
{
flux[par[nod]][nod] += flow;
flux[nod][par[nod]] -= flow;
nod = par[nod];
}
rasp += flow;
}
g << rasp << '\n';
}
//-------------------------------------------
void citire()
{
int m;
f >> n >> m;
v.resize(n + 1);
for(int i = 1; i <= m; ++i)
{
int x,y,c;
f >> x >> y >> c;
cap[x][y] += c;
v[x].push_back(y);
v[y].push_back(x);
}
}