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. JavaScript--字符串常用方法总结

    JavaScript--字符串常用方法总结 举例模板: var str = "what are you " var str1 = "sss" var txt = ...

  2. CentOS 7.2最小化安装没有ifconfig命令,使用yum provides ifconfig找不到相关的包

    [root@sishen yum.repos.d]# yum provides ifconfig Loaded plugins: fastestmirror Loading mirror speeds ...

  3. 关于margin、padding 对内联元素的影响

    内联元素和块级元素的区别是新手必须要掌握的知识点.大家可能平时注意块级元素比较多.所以这里重点让我们来讲讲常见的width height margin  padding 对inline元素的影响. 测 ...

  4. 迭代器———更锋利的C#代码小记(3)

    直接使用yield return关键字通过类似返回值的方式灵活地构造迭代器 public class EmployeeCollection :IEnumerable<Employee> { ...

  5. 获取登陆信息 在created()方法中

    // 获取登录信息 public async InitUser() { await sj.globalVar.Init(true); this.params.unitId = sj.globalVar ...

  6. android 防止bitmap 内存溢出

    在android开发过程中经常会处理网络图片发送内存溢出,那么怎么解决这种问题? 思路: 下载到本地 通过网络获取和文件下载存放到手机中目录 代码: // 获取网络 public InputStrea ...

  7. Github-Client(ANDROID)开源之旅(三) ------ 巧用ViewPagerIndicator

    接上篇博文:Github-Client(ANDROID)开源之旅(二) ------ 浅析ActionBarSherkLock 文中结合了网易新闻客户端讲解了开源库ActionBarSherklock ...

  8. spring mvc 解决 Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 异常

    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...

  9. https为数据传输保驾护航

    为什么要使用https 谷歌官网已宣布,今年7月起,Chrome浏览器的地址栏将把所有HTTP标示为不安全网站. 在客户端与服务器数据传输过程中,http协议传输是不安全的,一般情况下,http协议的 ...

  10. 第16周翻译:SQL Server中的事务日志管理,级别3:事务日志、备份和恢复

    源自: http://www.sqlservercentral.com/articles/Stairway+Series/73779/ 作者: Tony Davis, 2011/09/07 翻译:刘琼 ...