题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532
最近在学网络流,学的还不好,先不写理解了,先放模板。。。
我觉得写得不错的博客:http://blog.csdn.net/smartxxyx/article/details/9293665/
1 #include<stdio.h> 2 #include<string.h> 3 #include<vector> 4 #define maxn 222 5 #define inf 0x3f3f3f3f 6 using namespace std; 7 struct edge{ 8 int to; 9 int cap; 10 int rev; 11 }; 12 vector<edge>e[maxn]; 13 int book[maxn]; 14 int dfs(int cur,int end,int flow){ 15 if(cur == end) 16 return flow; 17 book[cur] = 1; 18 for(int i = 0;i<e[cur].size();i++){ 19 edge &temp = e[cur][i]; 20 if(!book[temp.to] && temp.cap>0){ 21 int d = dfs(temp.to,end,min(flow,temp.cap)); 22 if(d > 0){ 23 temp.cap -= d; 24 e[temp.to][temp.rev].cap += d; 25 return d; 26 } 27 } 28 } 29 return 0; 30 } 31 int main(){ 32 int n,m; 33 while(scanf("%d%d",&n,&m)!=EOF){ 34 for(int i = 1;i<=n;i++) 35 e[i].clear(); 36 int u,v,w; 37 for(int i = 0;i<n;i++){ 38 scanf("%d%d%d",&u,&v,&w); 39 e[u].push_back((edge){v,w,e[v].size()}); 40 e[v].push_back((edge){u,0,e[u].size()-1}); 41 } 42 int ans = 0; 43 while(1){ 44 memset(book,0,sizeof(book)); 45 int temp = dfs(1,m,inf); 46 if(temp == 0) 47 break; 48 ans += temp; 49 } 50 printf("%d ",ans); 51 } 52 return 0; 53 }