Pagini recente » Cod sursa (job #375296) | Cod sursa (job #2224479) | Cod sursa (job #3205392) | Cod sursa (job #1172465) | Cod sursa (job #1164785)
#include<cstdio>
#include<queue>
using namespace std;
const int N=1000,INF=2000000000;
int nextv[N+1][N+1],flow[N+1][N+1];
queue<int>q;
int predPath[N+1];
bool vis[N+1];
int n,m;
void scan(){
int x,y,z,i;
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
nextv[x][y]=z;
}
}
void init(){
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scan();
}
void path(int val){
int x=n;
while(x!=1){
flow[predPath[x]][x]+=val;
flow[x][predPath[x]]-=val;
x=predPath[x];
}
}
int minimum(){
int m=INF,x=n;
while(x!=1){
m=min(m,nextv[predPath[x]][x]-flow[predPath[x]][x]);
x=predPath[x];
}
return m;
}
void reset(){
int i;
while(!q.empty())
q.pop();
for(i=1;i<=n;i++)
vis[i]=false;
}
bool bfs(){
int i;
reset();
q.push(1);
while(q.front()!=n&&!q.empty()){
for(i=1;i<=n;i++)
if(nextv[q.front()][i]-flow[q.front()][i]>0&&!vis[i]){
q.push(i);
vis[i]=true;
predPath[i]=q.front();
}
q.pop();
}
return q.front()==n;
}
void print(){
int i,sum=0;
for(i=1;i<n;i++)
if(nextv[i][n]>0)
sum+=flow[i][n];
printf("%d",sum);
}
int main(){
init();
while(bfs()){
m=minimum();
path(m);
}
print();
return 0;
}