XTU 二分图和网络流 练习题 J. Drainage Ditches
J. Drainage Ditches
64-bit integer IO format: %I64d Java class name: Main
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
Output
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的更多相关文章
- 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 ...
- XTU 二分图和网络流 练习题 C. 方格取数(1)
C. 方格取数(1) Time Limit: 5000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d Java class ...
- 【网络流】POJ1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78671 Accepted: 3068 ...
- 网络流 最大流 Drainage Ditches Dinic
hdu 1532 题目大意: 就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速,本题就是让你求出最大流速,无疑要运用到求最大流了 ...
- (网络流 模板 Dinic) Drainage Ditches --POJ --1273
链接: http://poj.org/problem?id=1273 代码: //Dinic #include<stdio.h> #include<string.h> #inc ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ1273 Drainage Ditches (网络流)
Drainage Ditches Time Limit: 1000MS Memor ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches
Drainage Ditches 题目抽象:给你m条边u,v,c. n个定点,源点1,汇点n.求最大流. 最好的入门题,各种算法都可以拿来练习 (1): 一般增广路算法 ford() #in ...
随机推荐
- PV,UV,IP概念
PV是网站分析的一个术语,用以衡量网站用户访问的网页的数量.对于广告主,PV值可预期它可以带来多少广告收入.一般来说,PV与来访者的数量成正比,但是PV并不直接决定页面的真实来访者数量,如同一个来访者 ...
- 【转】log4j的日志
一.Log4j配置 第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 放在src下的话就不用配置 否则得去web. ...
- input标签属性
很多时候,我们都用到了很多标签实现输入功能,所以在这里梳理一下. 1.建立一个文本框 <input type="text" name="userName" ...
- Java_面向对象中的this和super用法
this: 1.使用在类中,可以用来修饰属性.方法.构造器 2.表示当前对象或者是当前正在创建的对象 3.当形参与成员变量重名时,如果在方法内部需要使用成员变量,必须添加 this 来表明该变量时类成 ...
- ubuntu安装mysql多实例
想要尝试mysql的读写分离,在云上安装完mysql之后突然想到一个问题:我本机是没有公网IP的. 开始尝试在唯一一台云服务器上安装多个mysql实例. 主要步骤: 1.新建MySQL目录 (1):新 ...
- css:段落文本两端对齐
效果图: Css:
- 如何创建你的第一个手机APP?
本文使用helloworld来作为android的入门项目,通过这个最简单的项目来帮助大家了解android程序开发包含哪些部分,以及如何运行android程序,本次开发android程序的工具是ec ...
- iPhone4 offical AD
iPhone4 is so much more than just a new products.I mean this would have a lot of impact on the way i ...
- AJAX中文乱码解决方案
通过AJAX获取数据中文乱码解决方案: @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到 ...
- JavaScript 的垃圾回收与内存泄露
JavaScript采用垃圾自动回收机制,运行时环境会自动清理不再使用的内存,因此javascript无需像C++等语言一样手动释放无用内存. 在这之前先说一下垃圾回收的两种方式:引用计数与标记清除. ...