J. Drainage Ditches

Time Limit: 1000ms
Memory Limit: 32768KB

64-bit integer IO format: %I64d      Java class name: Main

 
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

解题:哈哈 直接求最大流就是了!模板一刷,AC到手。。。。。。。。^_^
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <queue>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int cap[maxn][maxn],flow[maxn][maxn],a[maxn],link[maxn];
queue<int>q;
int main(){
int n,m,i,j,u,v,w,ans;
while(~scanf("%d%d",&n,&m)){
memset(cap,,sizeof(cap));
memset(flow,,sizeof(flow));
for(i = ; i < n; i++){
scanf("%d%d%d",&u,&v,&w);
cap[u][v] += w;
}
while(!q.empty()) q.pop();
ans = ;
while(true){
memset(a,,sizeof(a));
a[] = INF;
q.push();
while(!q.empty()){
u = q.front();
q.pop();
for(v = ; v <= m; v++){
if(!a[v] && cap[u][v] > flow[u][v]){
link[v] = u;
q.push(v);
a[v] = min(a[u],cap[u][v]-flow[u][v]);
}
}
}
if(a[m] == ) break;
for(u = m; u != ; u = link[u]){
flow[link[u]][u] += a[m];
flow[u][link[u]] -= a[m];
}
ans += a[m];
}
printf("%d\n",ans);
}
return ;
}

Dinic大法好啊

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int e[maxn][maxn],d[maxn],S,T,N;
queue<int>q;
bool bfs() {
memset(d,-,sizeof(d));
q.push();
d[] = ;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = ; i <= T; i++) {
if(d[i] < && e[u][i] > ) {
d[i] = d[u]+;
q.push(i);
}
}
}
return d[T] > ;
}
int dfs(int u,int low) {
int a = ;
if(u == T) return low;
for(int i = ; i <= T; i++) {
if(e[u][i] > && d[i] == d[u]+ && (a = dfs(i,min(low,e[u][i])))) {
e[u][i] -= a;
e[i][u] += a;
return a;
}
}
return ;
}
int main() {
int u,v,w,ans,flow;
while(~scanf("%d %d",&N,&T)) {
memset(e,,sizeof(e));
ans = ;
for(int i = ; i < N; i++) {
scanf("%d %d %d",&u,&v,&w);
e[u][v] += w;
}
while(bfs()) while(flow = dfs(,INF)) ans += flow;
printf("%d\n",ans);
}
return ;
}

ISAP大法

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[];
int head[maxn],p[maxn],d[maxn],gap[maxn],cur[maxn];
int tot,S = ,T,q[],hd,tl;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
void bfs(){
memset(gap,,sizeof(gap));
memset(d,-,sizeof(d));
d[T] = ;
q[tl++] = T;
while(hd < tl){
int u = q[hd++];
++gap[d[u]];
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q[tl++] = e[i].to;
}
}
}
}
int isap(){
int maxFlow = ,flow = INF,u = S;
memcpy(cur,head,sizeof(head));
bfs();
while(d[S] < T){
int &i = cur[u];
for( ;~i; i = e[i].next)
if(e[i].flow && d[u] == d[e[i].to] + ) break;
if(i > -){
flow = min(flow,e[i].flow);
p[u = e[i].to] = i;
if(u == T){
do{
int v = p[u];
e[v].flow -= flow;
e[v^].flow += flow;
u = e[v^].to;
}while(u != S);
maxFlow += flow;
flow = INF;
}
}else{
if(--gap[d[u]] == ) break;
d[u] = T;
cur[u] = head[u];
for(int k = head[u]; ~k; k = e[k].next)
if(e[k].flow && d[e[k].to] + < d[u])
d[u] = d[e[k].to] + ;
++gap[d[u]];
if(u != S) u = e[p[u]^].to;
}
}
return maxFlow;
}
int main(){
int x,y,z,n;
while(~scanf("%d %d",&n,&T)){
memset(head,-,sizeof(head));
tot = ;
while(n--){
scanf("%d %d %d",&x,&y,&z);
add(x,y,z);
}
printf("%d\n",isap());
}
}

dinic链式前向星版

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int to,flow,next;
arc(int x = ,int y = ,int z = -) {
to = x;
flow = y;
next = z;
}
};
arc e[maxn];
int head[maxn],d[maxn],cur[maxn],tot,n,m;
void add(int u,int v,int flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs() {
queue<int>q;
memset(d,-,sizeof(d));
d[] = ;
q.push();
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] == -) {
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[n] > ;
}
int dfs(int u,int low) {
if(u == n) return low;
int tmp = ,a = ;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].flow > && d[e[i].to] == d[u] + &&(a = dfs(e[i].to,min(low,e[i].flow)))) {
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic() {
int ans = ;
while(bfs()) {
memcpy(cur,head,sizeof(head));
ans += dfs(,INF);
}
return ans;
}
int main() {
while(~scanf("%d %d",&m,&n)) {
memset(head,-,sizeof(head));
int u,v,w;
for(int i = tot = ; i < m; ++i) {
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
printf("%d\n",dinic());
}
return ;
}

递归sap

 #include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>;
const int maxn = ;
struct arc {
int to,flow,next;
arc(int x = ,int y = ,int z = -) {
to = x;
flow = y;
next = z;
}
} e[maxn*maxn];
int head[maxn],d[maxn],gap[maxn],cur[maxn],tot,S,T,n;
void add(int u,int v,int flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
int sap(int u,int low) {
if(u == T) return low;
int tmp = ,a,minh = n - ;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].flow) {
if(d[u] == d[e[i].to] + &&(a = sap(e[i].to,min(low,e[i].flow)))) {
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(!low) break;
}
minh = min(minh,d[e[i].to]);
if(d[S] >= n) return tmp;
}
}
if(!tmp) {
if(--gap[d[u]] == ) d[S] = n;
d[u] = minh + ;
++gap[d[u]];
}
cur[u] = head[u];
return tmp;
}
int main() {
int u,v,w;
while(~scanf("%d%d",&n,&T)) {
memset(head,-,sizeof head);
memset(gap,,sizeof gap);
memset(d,,sizeof d);
tot = ;
for(int i = ; i < n; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
gap[S] = n;
memcpy(cur,head,sizeof cur);
int ret = ;
S = ;
while(d[S] < n) ret += sap(S,INF);
printf("%d\n",ret);
}
return ;
}

XTU 二分图和网络流 练习题 J. Drainage Ditches的更多相关文章

  1. XTU 二分图和网络流 练习题 B. Uncle Tom's Inherited Land*

    B. Uncle Tom's Inherited Land* Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I ...

  2. XTU 二分图和网络流 练习题 C. 方格取数(1)

    C. 方格取数(1) Time Limit: 5000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class ...

  3. 【网络流】POJ1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78671   Accepted: 3068 ...

  4. 网络流 最大流 Drainage Ditches Dinic

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

  5. (网络流 模板 Dinic) Drainage Ditches --POJ --1273

    链接: http://poj.org/problem?id=1273 代码: //Dinic #include<stdio.h> #include<string.h> #inc ...

  6. POJ 1273 Drainage Ditches(网络流,最大流)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  7. POJ1273 Drainage Ditches (网络流)

                                                             Drainage Ditches Time Limit: 1000MS   Memor ...

  8. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  9. 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches

    Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() #in ...

随机推荐

  1. 牛客网NOIP赛前集训营-普及组

    第一场: A-绩点 题目描述 小A刚考完大学考试.现在已经出了n门课的成绩,他想自己先算一下这些课的绩点是多少.设第i门课的他拿到的绩点是gpai,而这门课的学分是sci,那么他的总绩点用下面的公式计 ...

  2. json和php数组 格式的互相转换

    $json_arr = array('WebName'=>'PHP网站开发教程网','WebSite'=>'http://www.jb51.net');  $php_json = json ...

  3. 第一个 swift 项目

    今天 学习了 一丢丢 swift,特此记录一下 ! 原来创建的时候 ,只要把 语言 由以前的Object-C改为Swift,变创建好了自己的swift工程 第一个简单的swift demo 上代码 i ...

  4. Elasticsearch (2) - 映射

    常用映射类型 核心的字段类型如下: String 字符串包括text和keyword两种类型: 1.text analyzer 通过analyzer属性指定分词器. 下边指定name的字段类型为tex ...

  5. Maven项目中War包的打包及依赖方式

    两个web项目之间的依赖引用方式.Web项目之间,通过war包的方式进行引用的.例如,有两个项目,puzzle-web和puzzle-web-demo,两个均是web项目,puzzle-web-dem ...

  6. poj3436 Computer Factory

    题意: 电脑公司生产电脑有N个机器,每个机器单位时间产量为Qi. 电脑由P个部件组成,每个机器工作时只能把有某些部件的半成品电脑(或什么都没有的空电脑)变成有另一些部件的半成品电脑或完整电脑(也可能移 ...

  7. Dom 获取、Dom动态创建节点

    一.Dom获取 1.全称:Document     Object     Model 文档对象模型 2.我们常用的节点类型 元素(标签)节点.文本节点.属性节点(也就是标签里的属性). 3.docum ...

  8. vue中引入字体图标报错,找不到字体文件

    在用vue + webpack进行开发的时候,在引用字体图标遇到字体无法加载的问题: 报以下错误 搞了好久没搞定,最后才找到解决方法(还是没有找到原因) 修改字体图标的css中引入字体文件的路径 以前 ...

  9. 关于Android发送短信获取送达报告的问题

    最近公司开发一个项目,要求app能够发送短信并获取送达报告.这本不是一个什么难题,实现这一功能的代码一搜一大把,那么这么简单的一个问题,为什么我要在这里提出来呢?那是因为我在写代码的时候掉入了一个坑, ...

  10. IOS代码收集

    http://mobile.51cto.com/hot-410417.htm 退回输入键盘: - (BOOL) textFieldShouldReturn:(id)textField{ [textFi ...