Pagini recente » Cod sursa (job #1337389) | Cod sursa (job #2141317) | Cod sursa (job #975947) | Cod sursa (job #1828223) | Cod sursa (job #2692344)
#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>
#include <utility>
#define nmax 1000
using namespace std;
ifstream f ("maxflow.in");
ofstream g ("maxflow.out");
int n, m, max_flow;
vector< pair<int, int> > v[nmax+5];
vector<int> parinte(nmax+5);
vector<int> viz(nmax+5);
int gr[nmax+5][nmax+5];
queue <int> que;
bool bfs()
{
int ok=0;
while(!que.empty())
{
int nod=que.front();
if(nod!=n)
{
for(auto &i:v[nod])
{
int dest=i.first;
if(gr[nod][dest] && viz[dest]==0)
{
que.push(dest);
if(dest==n)
ok=1;
viz[dest]=1;
parinte[dest]=nod;
}
}
}
que.pop();
}
return ok;
}
int ford_fulkerson()
{
int u,q;
que.push(1);
while(bfs())
{
for(int i=0; i<v[n].size(); i++)
{
int sursa, capacitate;
sursa=v[n][i].first;
capacitate=v[n][i].second;
if(gr[sursa][n] && viz[sursa]==1 && gr[n][sursa]!=capacitate)
{
int nod=sursa;
int min_flow=gr[sursa][n];
while(nod!=1)
{
min_flow=min(min_flow, gr[parinte[nod]][nod]);
nod=parinte[nod];
}
nod=sursa;
gr[nod][n]-=min_flow;
gr[n][nod]+=min_flow;
while(nod!=1)
{
gr[parinte[nod]][nod]-=min_flow;
gr[nod][parinte[nod]]+=min_flow;
nod=parinte[nod];
}
max_flow+=min_flow;
}
}
que.push(1);
fill(parinte.begin(), parinte.end(), 0);
fill(viz.begin(), viz.end(), 0);
}
return max_flow;
}
vector<pair<int, int>> muchii;
int main()
{
int ok=1;
f>>n>>m;
for(int i=0; i<m; i++)
{
int x, y, z, t;
f>>x>>y>>z;
v[x].push_back(make_pair(y,z));
v[y].push_back(make_pair(x,z));
gr[x][y]=z;
}
g<<ford_fulkerson();
return 0;
}