Pagini recente » Cod sursa (job #2415332) | Cod sursa (job #570024) | Cod sursa (job #2395620) | Cod sursa (job #3200246) | Cod sursa (job #1164753)
#include<cstdio>
#include<queue>
#include<vector>
#define pb push_back
using namespace std;
const int MAX_N=1001;
const int INF=2000000000;
int f[MAX_N][MAX_N],c[MAX_N][MAX_N],pred[MAX_N];
int n,m,s,d,fmax,x,y,z;
vector<int> v[MAX_N];
queue<int>q;
int min2(int a,int b){
if(a<=b) return a;
else return b;
}
int minim(){
int m=INF,x=d;
while(x!=s){
m=min2(m,c[pred[x]][x]-f[pred[x]][x]);
x=pred[x];
}
return m;
}
void drum(int val){
int x=d;
while(x!=s){
f[pred[x]][x]+=val;
f[x][pred[x]]-=val;
x=pred[x];
}
}
bool bfs()
{
q.push(s);
for (int i=1; i<=n; ++i)
pred[i]=0; pred[s]=s;
while (q.size())
{
int nod=q.front(); q.pop();
if (nod==d) continue;
vector <int>::iterator it=v[nod].begin();
for (; it!=v[nod].end(); ++it)
if (!pred[*it] && c[nod][*it]>f[nod][*it])
pred[*it]=nod, q.push(*it);
}
return (pred[d]);
}
int main(){
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d",&n,&m);
s=1;d=n;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
c[x][y]=z;
v[x].pb(y);
v[y].pb(x);
}
while(bfs()){
m=minim();
drum(m);
}
for(int i=1;i<=n;i++)
fmax+=f[i][n];
printf("%d",fmax);
return 0;
}