Pagini recente » Cod sursa (job #635622) | Cod sursa (job #1360810) | Cod sursa (job #1218212) | Cod sursa (job #1981081) | Cod sursa (job #2759875)
#include <bits/stdc++.h>
#define nmax 1001
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
struct edge{
int s,t;
int c,f;
int real;
edge(){
c=-1;
}
};
vector<edge> graph[nmax];
int main()
{
int n,m;
f>>n>>m;
for(int i=0;i<m;i++)
{
int x,y,z;
edge e;
f>>x>>y>>z;
e.s=x;
e.t=y;
e.c=z;
e.f=0;
graph[x].push_back(e);
graph[x].back().real=graph[x].size()-1;
}
bool ok=1;
int ans=0;
while(ok)
{
ok=0;
queue<int> q;
vector<edge> rev(nmax);
q.push(1);
while(q.size()>0)
{
int curr=q.front();
q.pop();
for(edge e:graph[curr])
{
if(rev[e.t].c==-1&&e.c>e.f)
{
rev[e.t]=e;
q.push(e.t);
}
}
}
if(rev[n].c!=-1)
{
ok=1;
int mnf=1e9;
for(int i=n;i!=1;i=rev[i].s)
{
mnf=mnf>(rev[i].c-rev[i].f)?rev[i].c-rev[i].f:mnf;
//cout<<rev[i].c<<' '<<rev[i].f<<' '<<rev[i].s<<' '<<rev[i].t<<'\n';
}
//cout<<mnf<<'\n';
for(int i=n;i!=1;i=rev[i].s)
{
graph[rev[i].s][rev[i].real].f+=mnf;
}
ans+=mnf;
}
}
g<<ans;
return 0;
}