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. 在vue组件的stylus样式总 取消search类型的input按钮中默认样式

    在vue组件的stylus样式中 取消search类型的input按钮中默认样式 记录一个坑 环境 Vue组件 使用了Stylus的CSS风格. 问题 input输入框使用了 type="s ...

  2. 网站如何从http升级成https

    基本概念: HTTP: 是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准,用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少. HT ...

  3. iOS开发隐藏tabBar的问题

    开发中遇到第一个页面需要显示tabBar,但是第二个页面不需要显示,当回到第一个页面的时候又需要显示的情况. 在第一个页面跳转到第二个页面的时候需要给第二个页面设置tabBar的隐藏 - (void) ...

  4. PHP使用iconv函数遍历数组转换字符集

    /** * 字符串/二维数组/多维数组编码转换 * @param string $in_charset * @param string $out_charset * @param mixed $dat ...

  5. AJPFX总结jvm运行时内存分布

    jvm的运行过程中将java程序运行时数据区分为以下几个部分:      (1)程序计数器:存储虚拟机字节码执行的地址 (2)java虚拟机栈:java方法运行时的局部变量表,操作数栈,方法出口等 ( ...

  6. Java开发笔记(九十四)文件通道的性能优势

    前面介绍了字节缓存的一堆概念,可能有的朋友还来不及消化,虽然文件通道的用法比起传统I/O有所简化,可是平白多了个操控繁琐的字节缓存,分明比较传统I/O更加复杂了.尽管字节缓存享有缓存方面的性能优势,但 ...

  7. lavarel功能总结

    详细可参见笔记:laraval学习笔记(二) 路由 route 绑定模型,绑定参数 模版 blade .blade.php后缀,有laravel自己的模版语法 模型 model 如果用create创建 ...

  8. 字符串循环右移-c语言

    一个长度为len的字符串,对其循环右移n位 [期望]char str[] = "abcdefg";右移3次后,变成"efgabcd" [思路] 思路1. 如果用 ...

  9. PMP项目管理学习笔记(4)——项目整合管理

    六个整合管理过程. 1.制定项目章程 一个新项目要完成的第一件事,就是项目章程的制定.这是授权你开展工作的文档.不过并不总是需要你介入,通常情况下会由赞助人交给你.如果没有项目章程,你就没有权利告诉你 ...

  10. (转)让Spring自动扫描和管理Bean

    http://blog.csdn.net/yerenyuan_pku/article/details/52861403 前面的例子我们都是使用XML的bean定义来配置组件.在一个稍大的项目中,通常会 ...