| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
maxflow/*1273 Accepted 304K 16MS C++ 1372B */
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
int N,M;
int s,d,l;
int cnt[202][202];
int pre[202];
queue<int>q;
bool bfs(int src,int des){
memset(pre,-1,sizeof(pre));
while(!q.empty()){
q.pop();
}
q.push(src);
pre[src]=0;
while(!q.empty()){
int tmp=q.front();
q.pop();
for(int i=1;i<=M;i++){
if(pre[i]==-1&&cnt[tmp][i]>0){
pre[i]=tmp;
if(i==des){
return true;
}
q.push(i);
}
}
}
return false;
}
int MaxFlow(int src,int des){
int maxflow=0;
while(bfs(src,des)){
int minflow=inf;
for(int i=des;i!=src;i=pre[i]){
minflow=min(minflow,cnt[pre[i]][i]);
}
for(int i=des;i!=src;i=pre[i]){
cnt[pre[i]][i]-=minflow;
cnt[i][pre[i]]+=minflow;
}
maxflow+=minflow;
}
return maxflow;
}
int main(){
while(scanf("%d%d",&N,&M)!=EOF){
for(int i=1;i<=M;i++){
for(int j=1;j<=M;j++){
cnt[i][j]=0;
}
}
for(int i=0;i<N;i++){
scanf("%d%d%d",&s,&d,&l);
cnt[s][d]+=l;
}
printf("%d\n",MaxFlow(1,M));
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator