Pagini recente » Cod sursa (job #932074) | Cod sursa (job #2082960) | Cod sursa (job #529912) | Cod sursa (job #1428992) | Cod sursa (job #2693818)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n,m,c[1001][1001],f[1001][1001],t[1001],Fmax;
vector<int> v[1001];
int bfs()
{
queue<int> q;
memset(t,0,sizeof(t));
t[1]=-1;
q.push(1);
while(!q.empty())
{
int nod=q.front();
q.pop();
if(nod==n)return 1;
for(int i=0;i<v[nod].size();i++)
{
int next=v[nod][i];
if(t[next]==0&&f[nod][next]!=c[nod][next])
{
t[next]=nod;
q.push(next);
}
}
}
return 0;
}
int main()
{
int x,y,cost;
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>x>>y>>cost;
v[x].push_back(y);
v[y].push_back(x);
c[x][y]=cost;
//c[y][x]=0;
}
while(bfs())
{
for(int i=0;i<v[n].size();i++)
{
int nod=v[n][i];
if(t[nod]&&f[nod][n]!=c[nod][n])
{
int FC=c[nod][n]-f[nod][n];
for(int j=nod;j!=1;j=t[j])
{
FC=min(FC,c[t[j]][j]-f[t[j]][j]);
}
if(FC)
{
Fmax+=FC;
f[nod][n]+=FC;
f[n][nod]-=FC;
for(int j=nod;j!=1;j=t[j])
{
f[t[j]][j]+=FC;
f[j][t[j]]-=FC;
}
}
}
}
}
fout<<Fmax;
}