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 ;
}

最新文章

  1. python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器
  2. 解决IE6下png图片透明度不显示的问题
  3. 阅读&lt;构建之法&gt;第13、14、15、16、17章 与 《一个程序员的生命周期》读后感
  4. 【转】jmeter 进行java request测试
  5. zend studio 10 字体,颜色,快捷键等相关设置
  6. 从一行代码里面学点JavaScript
  7. Move to Another Changelist
  8. Asp.net原理(第一篇)
  9. Linux web性能优化
  10. 关于android多点触控
  11. hdu1876(dp)
  12. redis win版安装
  13. linux c 使用socket 发送http请求 可以发送json格式数据
  14. C#读写Excel实践笔记
  15. java得到日期相减的天数
  16. docker 安装Nginx
  17. [LeetCode] 598. Range Addition II_Easy tag: Math
  18. Maven 解决 下载项目 compiler 为1.5的问题
  19. lazy-mock ,一个生成后端模拟数据的懒人工具
  20. CF13E Holes LCT

热门文章

  1. pyinstaller打包遇到的错误处理
  2. 启动azkaban时出现User xml file conf/azkaban-users.xml doesn&#39;t exist问题解决(图文详解)
  3. ubuntu安装mysql多实例
  4. npm安装使用及vue脚手架安装
  5. 无法登录phpmyadmin,报1130错误
  6. Android从图库选择照片
  7. 如何用sql server数据库恢复.bak数据库备份
  8. [整理]ADB命令行学习笔记
  9. 光线步进——RayMarching入门
  10. 使用SpringBoot-JPA进行自定义的保存及批量保存