Pagini recente » Cod sursa (job #809919) | Cod sursa (job #2276600) | Cod sursa (job #94837) | Cod sursa (job #1061448) | Cod sursa (job #1435790)
#include<cstdio>
using namespace std;
int ic,sf,coada[1001],pred[1001],a[1001][1001];
bool viz[1001];
int n,m;
bool bfs(int nod){
ic=1;sf=1;
coada[ic]=nod;
viz[nod]=true;
while(ic<=sf){
for(int i=1;i<=n;i++)
if(a[coada[ic]][i]!=0&&viz[i]==false){
sf++;
coada[sf]=i;
viz[i]=true;
pred[i]=coada[ic];
if(i==n)
return true;
}
ic++;
}
return false;
}
void update(){
int nod,val;
nod=coada[sf];
val=550000001;
while(pred[nod]!=0){
if(a[pred[nod]][nod]<val)
val=a[pred[nod]][nod];
nod=pred[nod];
}
nod=coada[sf];
while(pred[nod]!=0){
a[pred[nod]][nod]-=val;
a[nod][pred[nod]]+=val;
nod=pred[nod];
}
}
int main(){
int a1,b,c;
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&a1,&b,&c);
a[a1][b]=c;
}
while(bfs(1)==true){
for(int i=1;i<=n;i++)
viz[i]=false;
update();
}
int tot=0;
for(int i=1;i<=n;i++)
tot+=a[i][1];
printf("%d",tot);
return 0;
}