EK算法网络流模板hdu1532

来源:互联网 发布:淘宝代理怎么做 编辑:程序博客网 时间:2024/06/10 00:08

hdoj 1532是一道可以作为模板题目练手。

模板代码:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <cstdio>  
  2. #include <cstring>  
  3. #include <iostream>  
  4. #include <string>  
  5. #include <algorithm>  
  6. #include <map>  
  7. #include <vector>  
  8. using namespace std;  
  9. const int N = 1100;  
  10. const int INF = 0x3f3f3f3f;  
  11.   
  12. struct Node  
  13. {  
  14.     int to;//终点  
  15.     int cap; //容量  
  16.     int rev;  //反向边  
  17. };  
  18.   
  19. vector<Node> v[N];  
  20. bool used[N];  
  21.   
  22. void add_Node(int from,int to,int cap)  //重边情况不影响  
  23. {  
  24.     v[from].push_back((Node){to,cap,v[to].size()});  
  25.     v[to].push_back((Node){from,0,v[from].size()-1});  
  26. }  
  27.   
  28. int dfs(int s,int t,int f)  
  29. {  
  30.     if(s==t)  
  31.         return f;  
  32.     used[s]=true;  
  33.     for(int i=0;i<v[s].size();i++)  
  34.     {  
  35.         Node &tmp = v[s][i];  //注意  
  36.         if(used[tmp.to]==false && tmp.cap>0)  
  37.         {  
  38.             int d=dfs(tmp.to,t,min(f,tmp.cap));  
  39.             if(d>0)  
  40.             {  
  41.                 tmp.cap-=d;  
  42.                 v[tmp.to][tmp.rev].cap+=d;  
  43.                 return d;  
  44.             }  
  45.         }  
  46.     }  
  47.     return 0;  
  48. }  
  49.   
  50. int max_flow(int s,int t)  
  51. {  
  52.     int flow=0;  
  53.     for(;;){  
  54.         memset(used,false,sizeof(used));  
  55.         int f=dfs(s,t,INF);  
  56.         if(f==0)  
  57.             return flow;  
  58.         flow+=f;  
  59.     }  
  60. }  
  61. int main()  
  62. {  
  63.     int n,m;  
  64.     while(~scanf("%d%d",&n,&m))  
  65.     {  
  66.         memset(v,0,sizeof(v));  
  67.         for(int i=0;i<n;i++)  
  68.         {  
  69.             int x,y,z;  
  70.             scanf("%d%d%d",&x,&y,&z);  
  71.             add_Node(x,y,z);  
  72.         }  
  73.         printf("%d\n",max_flow(1,m));  
  74.     }  
  75. }  

0 0
原创粉丝点击