pid=1532">Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8574    Accepted Submission(s): 3991

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

题目大意:

就是因为下大雨的时候约翰的农场就会被雨水给淹没。无奈下约翰不得不修建水沟,并且是网络水沟,并且聪明的约翰还控制了水的流速,本题就是让你求出最大流速,无疑要运用到求最大流了。

题中m为水沟数。n为水沟的顶点,接下来Si,Ei,Ci各自是水沟的起点,终点以及其容量。求源点1到终点n的最大流速。

注意重边

</pre></p><pre name="code" class="cpp">
EdmondsKarp算法写的:
邻接矩阵:
</pre><pre name="code" class="cpp">
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f using namespace std; const int M = 1000 + 50;
int n, m;
int r[M][M];
int pre[M];// 记录结点i的前向结点为pre[i]
bool vist[M];// 记录结点i是否已訪问 bool BFS(int s, int t) //推断是否存在增广路
{
queue<int>que;
memset(pre, 0, sizeof(pre));
memset(vist, false, sizeof(vist));
pre[s] = s;
vist[s] = true;
que.push(s);
int p;
while( !que.empty() )
{
p = que.front();
que.pop();
for(int i=1; i<=n; i++)
{
if(r[p][i]>0 && !vist[i])
{
pre[i]=p;
vist[i]=true;
if( i==t )
return true;
que.push(i);
}
}
}
return false;
} int EK(int s, int t)
{
int maxflow = 0;
while( BFS(s, t) )
{
int d = INF;
// 若有增广路径,则找出最小的delta
for(int i=t; i!=s; i=pre[i])
d = min(d, r[ pre[i] ][i]);
// 这里是反向边
for(int i=t; i!=s; i=pre[i])
{
r[ pre[i] ][i] -= d;//方向边
r[i][ pre[i] ] += d;//方向边
}
maxflow += d;
}
return maxflow;
} int main()
{
while(cin>>m>>n)
{
memset(r, 0, sizeof(r));
for(int i=0; i<m; i++)
{
int from, to, rap;
scanf("%d%d%d", &from, &to, &rap);
r[from][to] += rap;
}
cout<<EK(1, n)<<endl;
}
return 0;
}

邻接表(紫书上的模板):

#include<cstdio>

#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue> using namespace std; #define INF 0x3f3f3f3f;
const int MAXN = 1000 + 50; struct Edge
{
int from, to, cap, flow;
Edge (int u, int v, int c, int f):from(u), to(v), cap(c), flow(f) {}
}; struct EdmondsKarp
{
int n, m;
vector<Edge> edges;
vector<int> G[MAXN];
int a[MAXN];
int p[MAXN]; void init(int n)
{
for(int i=0; i<n; i++)
G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap)
{
edges.push_back( Edge(from, to, cap, 0) );
edges.push_back( Edge(to, from, 0, 0) );
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
} int Maxflow(int s, int t)
{
int flow = 0;
for( ; ; )
{
memset(a, 0, sizeof(a));
queue<int> Q;
Q.push(s);
a[s]=INF;
while( !Q.empty() )
{
int x = Q.front();
Q.pop();
for(int i=0; i<G[x].size(); i++)
{
Edge& e = edges[ G[x][i] ];
if( !a[e.to] && e.cap > e.flow )
{
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap-e.flow);
Q.push(e.to);
}
}
if( a[t] ) break;
}
if( ! a[t] ) break;
for(int u=t; u!=s; u=edges[ p[u] ].from )
{
edges[ p[u] ].flow += a[t];
edges[ p[u]^1 ].flow -= a[t];
}
flow += a[t];
}
return flow;
} }; int main()
{
EdmondsKarp T;
int n, m;
while(scanf("%d%d", &m, &n) !=EOF)
{
T.init(n+1);
for(int i=0; i<m; i++)
{
int a1, a2, a3;
scanf("%d%d%d", &a1, &a2, &a3);
T.AddEdge(a1, a2, a3);
}
printf("%d\n", T.Maxflow(1, n));
} return 0;
}

Dinic算法:

#include <cstdio>
#include <cstring>
#include <queue>
#define MAXN 205
#define INF 1000000000
using namespace std;
struct Edge {
int from, to, cap, flow;
}; struct Dinic {
int n, m, s, t;
vector<Edge> edges; //边表.edges[e]和edges[e^1]互为反向弧
vector<int> G[MAXN]; //邻接表。G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[MAXN]; //BFS使用
int d[MAXN]; //从起点到i的距离
int cur[MAXN]; //当前弧指针 void ClearAll(int n) {
for (int i = 0; i < n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap) {
edges.push_back((Edge) {from, to, cap, 0});
edges.push_back((Edge) {to, from, 0, 0});
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
} bool BFS() {//使用BFS计算出每个点在残量网络中到t的最短距离d.
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s] = 1;
d[s] = 0;
while (!Q.empty()) {
int x = Q.front(); Q.pop();
for (int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) { //仅仅考虑残量网络中的弧
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x, int a) {//使用DFS从S出发,沿着d值严格递减的顺序进行多路增广。
if (x == t || a == 0) return a;
int flow = 0, f;
for (int& i = cur[x]; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
} int Maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} };
Dinic g;
int main()
{
int n, m, i, a, b, c;
while (~scanf("%d%d", &m, &n)) {
g.ClearAll(n + 1);
for (i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
g.AddEdge(a, b, c);
}
int flow = g.Maxflow(1, n);
printf("%d\n", flow);
}
return 0;
}

HDU 1532||POJ1273:Drainage Ditches(最大流)的更多相关文章

  1. 【47.63%】【hdu 1532】Drainage Ditches

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

  2. poj-1273 Drainage Ditches(最大流基础题)

    题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted ...

  3. POJ-1273 Drainage Ditches 最大流Dinic

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...

  4. POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

    http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...

  5. poj1273 Drainage Ditches (最大流板子

    网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...

  6. [poj1273]Drainage Ditches(最大流)

    解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  7. poj1273 Drainage Ditches Dinic最大流

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76000   Accepted: 2953 ...

  8. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  9. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

随机推荐

  1. 如何让Jboss的debug在myeclise上运行

    1.在windows下运行jboss的debug.bat 看见监听的端口 2.打开myeclipse 点击选择 ①你要配置的名字(随意) ②myeclipse中选中该项目 ③jboss的启动的ip地址 ...

  2. xmllint命令

    xmllint是一个很方便的处理及验证xml的工具,linux下只要安装libxml2就可以使用这个命令,下面整理一些常用功能 1. --format 此参数用于格式化xml,使其具有良好的可读性. ...

  3. thinkphp 多个字段的不同关系的查询条件实现 .

    tp的$map不同条件默认是 and ,如果要用or<><><><>如下 例如查询Stu表中年龄大于18,或者身高低于180cm的男性(1为男性),(例 ...

  4. ASP.NET-常用正则表达式

    常用正则表达式 正则: [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}", ErrorMes ...

  5. EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER

    EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER ...

  6. hello world to php( mac 配置 xmapp virtual host)

    一.安装xmapp.安装完以后查看,服务是否都能启动(数据库和server) 二.配置自己的virtualhost       1.系统host文件加入server的域名(在浏览器中输入域名后会先通过 ...

  7. leetcode第一刷_Reverse Linked List II

    翻转链表绝对是终点项目,应该掌握的,这道题要求的是翻转一个区间内的节点.做法事实上非常相似,仅仅只是要注意判定開始是头的特殊情况,这样head要更新的,还有就是要把翻转之后的尾部下一个节点保存好,要么 ...

  8. [NOI.AC#32]sort 构造

    链接 50分做法(只有0,1) 根据归并排序的思想,假设我们现在已经把 \(l\dots mid\) 和 \(mid+1\dots r\) 排好序 只要把左边连续的1和右边连续的0翻转即可 inlin ...

  9. bzoj3444: 最后的晚餐(并查集+组合数学)

    3444: 最后的晚餐 题目:传送门 题解: 考虑有解的情况: 直接上并查集,同一个联通块里的人一定要坐在一起的.不难发现其实对于每个联通块最多就只有两种排列方式,那就直接把大于等于两个人的联通块先去 ...

  10. centos6.0 配置SVN

    基本步骤: 1.安装必需的subversion 2.创建版本库 3.配置用户和权限 4.钩子和svn常用命令说明 一.安装subversion 在这里我们使用yum来安装subversion,使用以下 ...