[模板]网络最大流 & 最小费用最大流
我的作业部落有学习资料
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 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
随机推荐
- Metasploit自动攻击模块
Metasploit自动攻击模块 Usage: db_autopwn [options] -h Display this help text -t Show all matching exploit ...
- saltstack的高级管理
一.saltstack的状态管理 状态管理官网: https://www.unixhot.com/docs/saltstack/ref/states/all/index.html 1)状态分析 [ro ...
- 关于iframe跨页面设置高度
注意:这两种方式不支持跨域使用 1.jQuery简单实现iframe的高度根据页面内容自适应的方法(加载后展示使用) 方式1: //注意:下面的代码是放在和iframe同一个页面中调用 $(" ...
- photoshop中调整图层的颜色深浅明暗
图像-调整-可选颜色, 选中某一个颜色如绿色,可以将绿色调的深一点或浅一点
- https://blog.csdn.net/eguid_1/article/category/6270094
https://blog.csdn.net/eguid_1/article/category/6270094
- zabbix入门之添加主机
添加主机的方法有两种:手动添加.自动发现 前提是:在被监控主机中安装zabbix-agent.zabbix-sender组件,并配置好启动服务. 手动添加: 自动发现: 这里等待1分钟左右即可发现主机 ...
- demo板 apt-get install stress
demo 那个网口 没有绑定mac 大电脑绑定了mac 大电脑上网认证系统:http://1.1.1.2 大电脑mac:6C-4B-90-3C-D5-7B 将demo板的mac改为大电脑mac ifc ...
- ltp-ddt nand_mtd_dd_rw_jffs2
error: 由于在uboot下没有发现坏块,将核心代码剥离出来调试: flash_eraseall -q -j /dev/mtd1mkdir -p /mnt/partition_nand_1419m ...
- [Luogu1821][USACO07FEB]银牛派对Silver Cow Party
由题意可知,我们需要求的是很多个点到同一个店的最短距离,然后再求同一个点到很多个点的最短距离. 对于后者我们很好解决,就是很经典的单源最短路径,跑一边dijkstra或者SPFA即可. 然而对于前者, ...
- TP、FP、FN、TN的含义
true positive(被正确分类的正例) false negative(本来是正例,错分为负例) true negative(被正确分类的负例) false positive(本来是负例,被错分 ...