HD1532Drainage Ditches(最大流模板裸题 + 邻接表)

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13273    Accepted Submission(s): 6288

Problem Description

Every time it rains on Farmer John‘s fields, a pond forms over Bessie‘s favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie‘s clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10

Sample Output

50

第一道最大流题目!

 1 #include <iostream>
 2 #include <queue>
 3 #include<string.h>
 4 using namespace std;
 5 #define arraysize 201
 6 int maxData = 0x7fffffff;
 7 int capacity[arraysize][arraysize]; //记录残留网络的容量
 8 int flow[arraysize];                //标记从源点到当前节点实际还剩多少流量可用
 9 int pre[arraysize];                 //标记在这条路径上当前节点的前驱,同时标记该节点是否在队列中
10 int n,m;
11 queue<int> myqueue;
12 int BFS(int src,int des)
13 {
14     int i,j;
15     while(!myqueue.empty())       //队列清空
16         myqueue.pop();
17     for(i=1;i<m+1;++i)
18     {
19         pre[i]=-1;
20     }
21     pre[src]=0;
22     flow[src]= maxData;
23     myqueue.push(src);
24     while(!myqueue.empty())
25     {
26         int index = myqueue.front();
27         myqueue.pop();
28         if(index == des)            //找到了增广路径
29             break;
30         for(i=1;i<m+1;++i)
31         {
32             if(i!=src && capacity[index][i]>0 && pre[i]==-1)
33             {
34                  pre[i] = index; //记录前驱
35                  flow[i] = min(capacity[index][i],flow[index]);   //关键:迭代的找到增量
36                  myqueue.push(i);
37             }
38         }
39     }
40     if(pre[des]==-1)      //残留图中不再存在增广路径
41         return -1;
42     else
43         return flow[des];
44 }
45 int maxFlow(int src,int des)
46 {
47     int increasement= 0;
48     int sumflow = 0;
49     while((increasement=BFS(src,des))!=-1)
50     {
51          int k = des;          //利用前驱寻找路径
52          while(k!=src)
53          {
54               int last = pre[k];
55               capacity[last][k] -= increasement; //改变正向边的容量
56               capacity[k][last] += increasement; //改变反向边的容量
57               k = last;
58          }
59          sumflow += increasement;
60     }
61     return sumflow;
62 }
63 int main()
64 {
65     int i,j;
66     int start,end,ci;
67     while(cin>>n>>m)
68     {
69         memset(capacity,0,sizeof(capacity));
70         memset(flow,0,sizeof(flow));
71         for(i=0;i<n;++i)
72         {
73             cin>>start>>end>>ci;
74             if(start == end)               //考虑起点终点相同的情况
75                continue;
76             capacity[start][end] +=ci;     //此处注意可能出现多条同一起点终点的情况
77         }
78         cout<<maxFlow(1,m)<<endl;
79     }
80     return 0;
81 }

刘汝佳书上给的邻接表算法

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <queue>
 6 #include <vector>
 7 #include <string.h>
 8 using namespace std;
 9 const int MAX = 210;
10 const int INF = 0x3f3f3f3f;
11 struct Edge
12 {
13     int from,to,cap,flow;
14     Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
15 };
16 int n,m;
17 vector<Edge> edge;
18 vector<int> g[MAX];
19 int a[MAX],p[MAX];
20 void init()
21 {
22     for(int i = 0; i <= m; i++)
23         g[i].clear();
24     edge.clear();
25 }
26 int Maxflow(int s,int t)
27 {
28     int flow = 0;
29     while(true)
30     {
31         memset(a,0,sizeof(a));
32         queue<int> q;
33         q.push(s);
34         a[s] = INF;
35         while(q.size())
36         {
37             int x = q.front();
38             q.pop();
39             int len = g[x].size();
40             for(int i = 0; i < len; i++)
41             {
42                 Edge e = edge[ g[x][i] ];
43                 if(a[e.to] == 0 && e.cap > e.flow)
44                 {
45                     p[e.to] = g[x][i];
46                     a[e.to] = min(a[x],e.cap - e.flow);
47                     q.push(e.to);
48                 }
49             }
50             if(a[t])
51                 break;
52         }
53         if(a[t] == 0)
54             break;
55         for(int i = t; i != s; i = edge[ p[i] ].from)
56         {
57             edge[ p[i] ].flow += a[t];
58             edge[ p[i] ^ 1 ].flow -= a[t];
59         }
60         flow += a[t];
61     }
62     return flow;
63 }
64 int main()
65 {
66     while(scanf("%d%d", &n,&m) != EOF)
67     {
68         int s,e,v,t;
69         init();
70         for(int i = 1; i <= n; i++)
71         {
72             scanf("%d%d%d",&s,&e,&v);
73             edge.push_back(Edge(s,e,v,0));
74             edge.push_back(Edge(e,s,0,0));
75             t = edge.size();
76             g[s].push_back(t - 2);
77             g[e].push_back(t - 1);
78         }
79
80         printf("%d\n",Maxflow(1,m));
81     }
82 }

时间: 2024-10-13 07:10:12

HD1532Drainage Ditches(最大流模板裸题 + 邻接表)的相关文章

POJ1273 Drainage Ditches 最大流模板题(dinic)

最大流的模板题 给出边数M,顶点数N 以及每条边的容量 求1到N的最大流 注意可以有重边 邻接矩阵模板: #include<iostream> #include<cstdio> #include<cstring> #define maxx 0x3f3f3f #define M 205 using namespace std; int arc[M][M]; //弧的剩余流量 int level[M]; int n; int min(int a,int b) { retur

HDU 1532Drainage Ditches(最大流模板题 ISAP)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11306    Accepted Submission(s): 5328 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie'

算法模板——sap网络最大流 3(递归+邻接表)

实现功能:同前 程序还是一如既往的优美,虽然比起邻接矩阵的稍稍长了那么些,不过没关系这是必然,但更重要的一个必然是——速度将是一个质的飞跃^_^(这里面的point指针稍作了些创新——anti指针,这个指向当前弧的反向弧,便于路径增广时的操作,相比非递归里面非要用一个op函数来挨个找已经强多了!!!) 1 type 2 point=^node; 3 node=record 4 g,w:longint; 5 anti,next:point; 6 end; 7 var 8 i,j,k,l,m,n,a

POJ1273 最大流EK 裸题

1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 int n,m; 7 const int maxn = 205; 8 int c[maxn][maxn],flow[maxn][maxn]; 9 int vis[maxn],fa[maxn]; 10 int largest_flow(int s,

POJ 1273 Drainage Ditches | 最大流模板

1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<queue> 5 #define N 210 6 #define INF 1234567897 7 using namespace std; 8 int cnt=2,head[N],n,m,ans,lev[N]; 9 queue <int> q; 10 struct edge 11 { 12 int u,

poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)

最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; // 裸最大流 const int N = 2005; const int M = 2005; const int INF = 0x7fffffff; // 邻接表 struct Ed

【POJ2425】A Chess Game 博弈,SG函数,裸题,模板题

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42653921 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 题意:给一个有向无环图(拓扑图),有若干个棋子,两人轮流操作,每次可以把其中某棋子沿图走一步,无法操作者输. 题解:SG函数裸题,模板题 代码: #include <cstdio> #include <cstring> #include <iostream> #include <a

hdu 3549 Flow Problem(最大流模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input

POJ 1087 最大流裸题 + map

A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15597   Accepted: 5308 Description You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an int