[模板]网络最大流 & 最小费用最大流
我的作业部落有学习资料
Dinic 模板
#define rg register
#define _ 10001
#define INF 2147483647
#define min(x,y) (x)<(y)?(x):(y)
using namespace std;
int n,m,s,t,record[_],num_of_edges=-,cur[_],team[],depth[_];
struct pp
{
int next,to,w;
}edge[(_<<)+(_<<)];
inline int read()
{
rg int save=,w=;rg char q=getchar();
while(q<''||q>''){if(q=='-')w=-;q=getchar();}
while(q>=''&&q<='')save=(save<<)+(save<<)+q-'',q=getchar();
return save*w;
}
inline void add(rg int from,rg int to,rg int ww)
{
edge[++num_of_edges]=(pp){record[from],to,ww};
record[from]=num_of_edges;
}
inline bool bfs()
{
rg int head=,tail=;
for(rg int i=;i<=n;++i)depth[i]=;//depth[]初值为-1,免得返回出发点
depth[s]=;
team[]=s;
do
{
head++;
rg int u=team[head];
for(rg int j=record[u];j!=-;j=edge[j].next)
{
rg int to=edge[j].to;
if((!depth[to])&&(edge[j].w>))
{
depth[to]=depth[u]+;
team[++tail]=to;
if(to==t)return ;
}
}
}while(head<tail);
return ;
}
int dfs(rg int u,rg int flow)//找到每条路上的最小残余流量,因此需在回溯时更新每条边的流量
{
if(u==t)return flow;
for(rg int &j=cur[u]/*当前弧优化*/;j!=-;j=edge[j].next)
{
rg int to=edge[j].to;
if(depth[to]==depth[u]+&&(edge[j].w>))
{
rg int D=dfs(to,min(flow,edge[j].w));//到终点的过程中一直在取min
if(D>)
{
edge[j].w-=D,edge[j^].w+=D;
return D;//每次只找一条可流通路满上(这里就不管现实中物理的联通器原理了)
} }
}
return ;
}
inline int Dinic()
{
rg int i,j,ans=;
while(bfs())
{
for(i=;i<=n;++i)cur[i]=record[i];
while(int now=dfs(s,INF))ans+=now;
}
return ans;
}
int main()
{
n=read(),m=read(),s=read(),t=read();
rg int i,j;
for(i=;i<=n;++i)record[i]=-;
for(i=;i<=m;++i)
{
rg int x=read(),y=read(),ww=read();
add(x,y,ww),add(y,x,);
}
printf("%d\n",Dinic());
return ;
}
最小费用最大流:
#define rg register
#define _ 5001
#define __ 50001
#define INF 2147483647
using namespace std;
int n,m,s,t,record[_],num_of_edges=,pre[_],res_flow,res_cost,dis[_],team[__<<];//以后num_of_edges都赋为 1 !!!
bool exist[_];
struct pp
{
int next,to,w,cost;
}edge[__<<];
inline int read()
{
rg int save=,w=;rg char q=getchar();
while(q<''||q>''){if(q=='-')w=-;q=getchar();}
while(q>=''&&q<='')save=(save<<)+(save<<)+q-'',q=getchar();
return w*save;
}
inline void add(rg int from,rg int to,rg int ww,rg int f)
{
edge[++num_of_edges]=(pp){record[from],to,ww,f};
record[from]=num_of_edges;
}
inline bool SPFA()
{
rg int head=,tail=;
for(rg int i=;i<=n;++i)exist[i]=,dis[i]=INF;
dis[s]=;
team[]=s;
do
{
head++;
rg int i=team[head];
exist[i]=;
for(rg int j=record[i];j;j=edge[j].next)
{
rg int to=edge[j].to;
if(edge[j].w>&&dis[to]>dis[i]+edge[j].cost)
{
dis[to]=dis[i]+edge[j].cost;
pre[to]=j;
if(!exist[to])team[++tail]=to,exist[to]=;
}
}
}while(head<tail);
return dis[t]!=INF;
}
inline void doit()
{
while(SPFA())
{
rg int flow=INF;
for(rg int i=pre[t];i;i=pre[edge[i^].to])
flow=min(flow,edge[i].w);
res_flow+=flow;
res_cost+=dis[t]*flow;
for(rg int i=pre[t];i;i=pre[edge[i^].to])
edge[i].w-=flow,edge[i^].w+=flow;
}
printf("%d %d\n",res_flow,res_cost);
}
int main()
{
n=read(),m=read(),s=read(),t=read();
rg int i,j;
for(i=;i<=m;++i)
{
rg int u=read(),v=read(),w=read(),f=read();
add(u,v,w,f),add(v,u,,-f);
}
doit();//实在不想用那个MCMF()
return ;
}
[模板]网络最大流 & 最小费用最大流的更多相关文章
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然 后跑最小费用最大流就OK了. ---- ...
- bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】
第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...
- 最大流 && 最小费用最大流模板
模板从 这里 搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...
- BZOJ1834 [ZJOI2010]network 网络扩容(最小费用最大流)
挺直白的构图..最小费用最大流的定义. #include<cstdio> #include<cstring> #include<queue> #include< ...
- 洛谷P2604 最大流+最小费用最大流
题目链接:https://www.luogu.org/problem/P2604 题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在 ...
- POJ - 2516 Minimum Cost(最小费用最大流)
1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- 洛谷P3381 最小费用最大流模板
https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
随机推荐
- C# 面试 笔试题
1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 private : 私有成员, 在类的内部才可以访问. ...
- 分析abex'crackme#1
测试文件下载:https://www.wocloud.com.cn/webclient/share/sindex.action?id=i9K_Br6TgE7Kf_YTF04yHmKcRy5TUdZ8U ...
- 【学习总结】Python-3-转义字符
参考: 本教程的评论区:菜鸟教程-Python3-Python数字 转义字符: 在需要在字符中使用特殊字符时,python用反斜杠()转义字符 END
- [转载]Redux原理(一):Store实现分析
写在前面 写React也有段时间了,一直也是用Redux管理数据流,最近正好有时间分析下源码,一方面希望对Redux有一些理论上的认识:另一方面也学习下框架编程的思维方式. Redux如何管理stat ...
- jenkins持续集成(一): 在Linux下的安装与配置
撸了今年阿里.网易和美团的面试,我有一个重要发现.......>>> 前提: 安装了JDK,并配置好环境变量:JAVA_HOME 安装了Maven,并配置好环境变量:MAVEN_HO ...
- java Map的四种遍历方式
1.这是最常见的并且在大多数情况下也是最可取的遍历方式,在键值都需要时使用. Map<Integer, Integer> map = new HashMap<Integer, Int ...
- centos 6.5 配置网络
编辑 vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改内容 DEVICE="eth0" BOOTPROTO="static& ...
- springboot实战(汪云飞)学习-1-1
java EE开发的颠覆者 spring boot 实战 随书学习-1 1.学习案例都是maven项目,首先要在eclipse 中配置 maven,主要修改maven的配置文件:配置文件下载链接: h ...
- 配置 Ceph 内外网分离
https://www.jianshu.com/p/42ab1f6dc6de 1. 为什么要做内外网分离 先明确一下这么做的必要性.Ceph 的客户端,如 RADOSGW,RBD 等,会直接和 O ...
- JS中的作用域及闭包
1.JS中的作用域 在 es6 出现之前JS中只有全局作用域和函数作用域,没有块级作用域,即 JS 在函数体内有自己的作用域,但是如果不是在函数体的话就全部都是全局作用域.比如在 if.for 等有 ...