HDU-1532 网络流裸题
题意简单的来说就是从1点到n点,最大的流量是多少。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define _S(X) cout << x << ' ';
#define __S(x) cout << x << endl;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = (int)1e9+;
const int N = ;
int head[N], w[N], to[N], nx[N];
int n, m, _u, _v, _w;
int tot, s, t;
int deep[N];
void add(int u, int v, int val){
w[tot] = val;
to[tot] = v;
nx[tot] = head[u];
head[u] = tot++;
} void init(){
memset(head, -, sizeof(head));
tot = ;
s = ;
t = n;
} int bfs(){
queue<int> q;
memset(deep, , sizeof(deep));
q.push(s);
deep[s] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = nx[i]){
if(w[i] > && deep[to[i]] == ){
deep[to[i]] = deep[u] + ;
q.push(to[i]);
}
}
}
if(deep[t] > ) return ;
return ;
}
int Dinic(int u, int flow){
if(u == t) return flow;
for(int i = head[u]; ~i; i = nx[i]){
if(deep[u]+ == deep[to[i]] && w[i] > ){
int di = Dinic(to[i], min(w[i], flow));
if(di > ){
w[i] -= di, w[i^] += di;
return di;
}
}
}
return ;
}
int main(){
while(~scanf("%d%d", &m, &n)){
init();
for(int i = ; i <= m; i++){
scanf("%d%d%d", &_u, &_v, &_w);
add(_u, _v, _w);
add(_v, _u, );
}
s = ; t = n;
int ans = , tmp;
while(bfs()){
while(tmp = Dinic(s, INF)) ans += tmp;
}
printf("%d\n", ans);
}
return ;
}
Dinic 还可以用当前弧优化,记录了当前处理到那一条边,就不需要重头再遍历那些l边。
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define _S(X) cout << x << ' ';
#define __S(x) cout << x << endl;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = (int)1e9+;
const int N = ;
int head[N], w[N], to[N], nx[N];
int n, m, _u, _v, _w;
int tot, s, t;
int deep[N], cur[N];
void add(int u, int v, int val){
w[tot] = val;
to[tot] = v;
nx[tot] = head[u];
head[u] = tot++;
} void init(){
memset(head, -, sizeof(head));
tot = ;
s = ;
t = n;
} int bfs(){
queue<int> q;
memset(deep, , sizeof(deep));
q.push(s);
deep[s] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = nx[i]){
if(w[i] > && deep[to[i]] == ){
deep[to[i]] = deep[u] + ;
q.push(to[i]);
}
}
}
if(deep[t] > ) return ;
return ;
}
int Dinic(int u, int flow){
if(u == t) return flow;
for(int &i = cur[u]; ~i; i = nx[i]){
if(deep[u]+ == deep[to[i]] && w[i] > ){
int di = Dinic(to[i], min(w[i], flow));
if(di > ){
w[i] -= di, w[i^] += di;
return di;
}
}
}
return ;
}
int main(){
while(~scanf("%d%d", &m, &n)){
init();
for(int i = ; i <= m; i++){
scanf("%d%d%d", &_u, &_v, &_w);
add(_u, _v, _w);
add(_v, _u, );
}
s = ; t = n;
int ans = , tmp;
while(bfs()){
for(int i = ; i <= n; i++) cur[i] = head[i];
while(tmp = Dinic(s, INF)) ans += tmp;
}
printf("%d\n", ans);
}
return ;
}
当前弧优化
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define _S(X) cout << x << ' ';
#define __S(x) cout << x << endl;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = (int)1e9+;
const int N = ;
int head[N], w[N], to[N], nx[N];
int vis[N], pre[N], id[N];
int n, m, _u, _v, _w;
int tot, s, t;
void add(int u, int v, int val){
w[tot] = val;
to[tot] = v;
nx[tot] = head[u];
head[u] = tot++;
} void init(){
memset(head, -, sizeof(head));
tot = ;
s = ;
t = n;
} int bfs(){
queue<int> q;
memset(vis, , sizeof(vis));
q.push(s);
vis[s] = ;
pre[s] = s;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = nx[i]){
if(!vis[to[i]] && w[i]){
pre[to[i]] = u;
id[to[i]] = i;
vis[to[i]] = ;
if(to[i] == t) return true;
q.push(to[i]);
}
}
}
return false;
}
int EK(){
int ans = ;
while(bfs()){
int mi = INF;
for(int i = t; i != s; i = pre[i]){
mi = min(mi, w[id[i]]);
}
for(int i = t; i != s; i = pre[i]){
w[id[i]] -= mi;
w[id[i]^] += mi;
}
ans += mi;
}
return ans;
}
int main(){
while(~scanf("%d%d", &m, &n)){
init();
for(int i = ; i <= m; i++){
scanf("%d%d%d", &_u, &_v, &_w);
add(_u, _v, _w);
add(_v, _u, );
}
s = ; t = n;
printf("%d\n", EK());
}
return ;
}
EK
HDU-1532 网络流裸题的更多相关文章
- HDU 1102 最小生成树裸题,kruskal,prim
1.HDU 1102 Constructing Roads 最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...
- 【网络流#3】hdu 1532 - Dinic模板题
输入为m,n表示m条边,n个结点 记下来m行,每行三个数,x,y,c表示x到y的边流量最大为c 这道题的模板来自于网络 http://blog.csdn.net/sprintfwater/articl ...
- 经典的最大流题POJ1273(网络流裸题)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- HDU 1248 寒冰王座(完全背包裸题)
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 2602 Bone Collector(01背包裸题)
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu Flow Problem (最大流 裸题)
最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...
- 【线性规划与网络流 24题】已完成(3道题因为某些奇怪的原因被抛弃了QAQ)
写在前面:SDOI2016 Round1滚粗后蒟蒻开始做网络流来自我拯救(2016-04-11再过几天就要考先修课,现在做网络流24题貌似没什么用←退役节奏) 做的题目将附上日期,见证我龟速刷题. 1 ...
- cogs_14_搭配飞行员_(二分图匹配+最大流,网络流24题#01)
描述 http://cojs.tk/cogs/problem/problem.php?pid=14 有一些正飞行员和副飞行员,给出每个正飞行员可以和哪些副飞行员一起飞.一架飞机上必须一正一副,求最多多 ...
随机推荐
- 百度网盘 人工智能书籍【Tensorflow和深度学习】
链接:https://pan.baidu.com/s/1ejCvwn08ILI2fMhBEdXR8w 提取码:6pk9
- 自练Eclipse搭建SSH全自动注解博客项目笔记
1.创建一个动态的java项目 2.导入搭建所需要的jar包 3.配置web.xml文件 1).头文件 2).struts2的拦截器 3).定位加载Spring容器的配置文件 4).监听 5). 6) ...
- AQS之CountDownLatch、Semaphore、CyclicBarrier
CountDownLatch A synchronization aid that allows one or more threads to wait until a set of operatio ...
- JMS入门简介
一.JMS是什么 1.JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中 ...
- Why do I write a blog
I believe the most beautiful and elegant answer to this question is from Churchill. "On a peace ...
- python3学习-Queue模块
python标准库中带有一个Queue模块,顾名思义,队列.该模块也衍生出一些基本队列不具有的功能. 我们先看一下队列的方法: put 存数据 get 取数据 empty 判断队列是否为空 qsize ...
- intellIJ IDEA学习笔记
如果你初次用idea,毫无目的的度娘如何使用IDEA 浪费的将会是大量的时间.为以表诚意, 上一套IDEA教学视频,以表我诚意.(下载地址:https://pan.baidu.com/s/1g ...
- Missing artifact XXXXX:jar:1.9.1 解决错误问题
昨天导过来一个maven工程的一个项目,由于自己meven库中有许多现成的jar包,但是还是有一些需要去下载的,配置的是阿里云的镜像,把eclise的预编译给关闭,具体做法为:Project----- ...
- Mysql: 图解 inner join、left join、right join、full outer join、union、union all的区别
转载来源 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚. ...
- opencv 视觉项目学习笔记(二): 基于 svm 和 knn 车牌识别
车牌识别的属于常见的 模式识别 ,其基本流程为下面三个步骤: 1) 分割: 检测并检测图像中感兴趣区域: 2)特征提取: 对字符图像集中的每个部分进行提取: 3)分类: 判断图像快是不是车牌或者 每个 ...