POJ-1273 Drainage Ditches 最大流裸题

  题目链接:http://poj.org/problem?id=1273

  最大流的裸题,纯水,EK都可以秒过。今天敲了一下ISAP,发现还不是很方便,感觉Dinic好敲多了,而且Dinic的效率又比较稳定,以后如果不是碰到那种特大的数据,还是敲Dinic吧。

  1 //STATUS:C++_AC_0MS_760KB

  2 #include<stdio.h>

  3 #include<stdlib.h>

  4 #include<string.h>

  5 #include<math.h>

  6 #include<iostream>

  7 #include<string>

  8 #include<algorithm>

  9 #include<vector>

 10 #include<queue>

 11 #include<stack>

 12 #include<map>

 13 using namespace std;

 14 #define LL long long

 15 #define Max(a,b) ((a)>(b)?(a):(b))

 16 #define Min(a,b) ((a)<(b)?(a):(b))

 17 #define mem(a,b) memset(a,b,sizeof(a))

 18 #define lson l,mid,rt<<1

 19 #define rson mid+1,r,rt<<1|1

 20 const int MAX=210,INF=0x3f3f3f3f;

 21 

 22 struct Edge{

 23     int from,to,cap,flow;

 24 };

 25 

 26 vector<Edge> edge;

 27 vector<int> g[MAX];

 28 int d[MAX],cur[MAX],vis[MAX];

 29 int n,m,s,t;

 30 

 31 int bfs()

 32 {

 33     int x,i;

 34     queue<int> q;

 35     q.push(s);

 36     mem(vis,0);

 37     d[s]=0;

 38     vis[s]=1;

 39     while(!q.empty())

 40     {

 41         x=q.front();q.pop();

 42         for(i=0;i<g[x].size();i++){

 43             Edge& e=edge[g[x][i]];

 44             if(!vis[e.to] && e.cap>e.flow){

 45                 vis[e.to]=1;

 46                 d[e.to]=d[x]+1;

 47                 q.push(e.to);

 48             }

 49         }

 50     }

 51     return vis[t];

 52 }

 53 

 54 int dfs(int x,int a)

 55 {

 56     if(x==t || a==0)return a;

 57     int f,flow=0;

 58     for(int& i=cur[x];i<g[x].size();i++){

 59         Edge& e=edge[g[x][i]];

 60         if(d[x]+1==d[e.to] && (f=dfs(e.to,Min(a,e.cap-e.flow)))>0){

 61             e.flow+=f;

 62             edge[g[x][i]^1].flow-=f;

 63             flow+=f;

 64             a-=f;

 65             if(!a)break;

 66         }

 67     }

 68     return flow;

 69 }

 70 

 71 int dinic()

 72 {

 73     int flow=0;

 74     while(bfs()){

 75         mem(cur,0);

 76         flow+=dfs(s,INF);

 77     }

 78     return flow;

 79 }

 80 

 81 int main()

 82 {

 83  //   freopen("in.txt","r",stdin);

 84     int i,j,u,v,val,ans;

 85     while(~scanf("%d%d",&m,&n))

 86     {

 87         s=1,t=n;

 88         for(i=1;i<=n;i++)g[i].clear();

 89         edge.clear();

 90         m<<=1;

 91         for(i=0;i<m;i+=2){

 92             scanf("%d%d%d",&u,&v,&val);

 93             edge.push_back((Edge){u,v,val,0});

 94             edge.push_back((Edge){v,u,0,0});

 95             g[u].push_back(i);

 96             g[v].push_back(i+1);

 97         }

 98 

 99         ans=dinic();

100 

101         printf("%d\n",ans);

102     }

103     return 0;

104 }

你可能感兴趣的:(poj)