这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧……

ek

bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int cap[][], pre[], n, m, a[];
int bfs(){
queue<int> q;
q.push();
memset(a,,sizeof(a));
a[] = INF;
while(!q.empty()){
int front = q.front();
q.pop();
for(int i = ;i<=m;i++){
if(!a[i] && cap[front][i]){
a[i] = min(a[front], cap[front][i]);
pre[i] = front;
q.push(i);
}
}
}
return a[m];
}
int ek(){
int ans = ;
while(bfs()){
ans += a[m];
for(int i = m;i!=;i = pre[i]){
cap[pre[i]][i] -= a[m];
cap[i][pre[i]] += a[m];
}
}
return ans;
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(cap,,sizeof(cap));
for(int i = ;i<=n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
cap[u][v] += w;
}
printf("%d\n",ek());
}
return ;
}

dinic

反复构造层次网络加找增广路,优势在于当某点的流入量较大时,可以一次完成多条增广路的累加,O(V^2E)

记得初始化lv,cnt=1

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct EDGE{
int to, next, cap, flow;
EDGE(){}
EDGE(int a, int b, int c, int d){
to = a, next = b, cap = c, flow = d;;
}
}e[];
int head[], cnt, lv[],m ,n;
void add(int from, int to, int cap){
e[++cnt] = EDGE(to, head[from], cap, );
head[from] = cnt;
e[++cnt] = EDGE(from, head[to], , );
head[to] = cnt;
}
int bfs(){
queue<int> q;
q.push();
memset(lv,,sizeof(lv));
lv[] = ;
while(!q.empty()){
int front = q.front();
q.pop();
for(int i = head[front];i;i = e[i].next){
int to = e[i].to;
if(!lv[to] && e[i].cap-e[i].flow){
lv[to] = lv[front]+;
q.push(to);
}
}
}
return lv[m];
}
int dfs(int now, int a){
int flow = ,f;
if(now == m) return a;
for(int i = head[now];i;i = e[i].next){
int to = e[i].to;
if(lv[to] == lv[now]+ && (f = dfs(to,min(a,e[i].cap-e[i].flow)))){
e[i].flow += f;
e[i^].flow -= f;
flow += f;
a -= f;
if(!a) break;
}
}
return flow;
}
int dinic(){
int ans = ;
while(bfs()){
ans += dfs(,INF);
}
return ans;
}
int main(){
while(~scanf("%d%d",&n,&m)){
cnt = ;
memset(head,,sizeof(head));
for(int i = ;i<=n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
printf("%d\n",dinic());
}
return ;
}

最新文章

  1. &lt;四&gt;JDBC_PreparedStatement的使用
  2. [poj2492]A Bug&#39;s Life(并查集+补集)
  3. LightOJ1017 Brush (III)(DP)
  4. Leetcode | Find Minimum in Rotated Sorted Array I &amp;&amp; II
  5. MSSQL系统进程锁GHOST CLEANUP
  6. 使用多种方式实现遍历HashMap
  7. poj 2230 Watchcow(欧拉回路)
  8. Android 读取txt文件并以utf-8格式转换成字符串
  9. C#的Split用法
  10. nodeJS实现简单网页爬虫功能
  11. C#实现导出Excel
  12. 深入理解Java虚拟机05--虚拟机类加载机制
  13. mysql 表结构
  14. C语言拼接字符串 -- 使用strcat()函数
  15. mybatis二级缓存应用及与ehcache整合
  16. UVA11417 GCD
  17. hdu 4968 最大最小gpa
  18. 关于分布式存储系统中-CAP原则(CAP定理)与BASE理论比较
  19. 黄聪:WordPress制作插件中使用wp_enqueue_script(&#39;jquery&#39;)库不起作用解决方法
  20. The Toll! Revisited UVA - 10537(变形。。)

热门文章

  1. (十八)JDBC优化使用(一)
  2. linux一行命令查杀进程
  3. 关于一些初学Unity的基本操作和自己的理解
  4. Net上传文件
  5. String字符串相加的原理
  6. 记一次Spring Cloud压力测试
  7. python学习-46 时间模块
  8. Anaconda安装报错
  9. 【满k叉树】Perfect Tree
  10. MogileFS操作指令