BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)
2024-10-20 16:20:28
题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604
(bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=1834
题解: 第一问所有的费用全按\(0\)建,跑完了之后很自然想到利用残余网络。
把\(n\)和一个新点\(T\)连边,然后原来的残量网络保留,在此基础上对于原来的每条边流量均按\(+\inf\)建,费用为原始费用再跑一遍即可。
时间复杂度\(O(MaxFlowMinCost(n,m))\)
(然而智障的我不会处理残量网路还想做\(K\)次费用流每次\(+1\),真是没脑子)
代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define llong long long
using namespace std;
const int N = 1002;
const int M = 10000;
const llong INF = 1000000000000ll;
struct Edge
{
int u,v,nxt,rev; llong c,w;
} e[(M<<1)+3];
Edge ae[M+3];
int fe[N+3];
int que[N+3];
llong dis[N+3];
bool inq[N+3];
int lst[N+3];
int n,m,en,p,s,t;
llong mf,mc;
void addedge(int u,int v,llong w,llong c)
{
en++; e[en].u = u; e[en].v = v; e[en].w = w; e[en].c = c;
e[en].nxt = fe[u]; fe[u] = en; e[en].rev = en+1;
en++; e[en].u = v; e[en].v = u; e[en].w = 0; e[en].c = -c;
e[en].nxt = fe[v]; fe[v] = en; e[en].rev = en-1;
}
bool spfa()
{
for(int i=1; i<=n; i++) dis[i] = INF;
int head = 1,tail = 2; que[tail-1] = s; dis[s] = 0ll;
while(head!=tail)
{
int u = que[head]; head++; if(head==n+1) head = 1;
for(int i=fe[u]; i; i=e[i].nxt)
{
if(e[i].w>0 && dis[e[i].v]>dis[u]+e[i].c)
{
dis[e[i].v] = dis[u]+e[i].c;
lst[e[i].v] = i;
if(!inq[e[i].v])
{
inq[e[i].v] = true;
que[tail] = e[i].v; tail++; if(tail==n+1) tail = 1;
}
}
}
inq[u] = false;
}
return dis[t]!=INF;
}
void calcflow()
{
llong flow = INF;
for(int i=t; i!=s; i=e[lst[i]].u)
{
flow = min(flow,e[lst[i]].w);
}
for(int i=t; i!=s; i=e[lst[i]].u)
{
e[lst[i]].w -= flow; e[e[lst[i]].rev].w += flow;
}
mf += flow; mc += flow*dis[t];
}
void mfmc()
{
while(spfa())
{
calcflow();
}
}
int main()
{
scanf("%d%d%d",&n,&m,&p);
for(int i=1; i<=m; i++)
{
scanf("%d%d%lld%lld",&ae[i].u,&ae[i].v,&ae[i].w,&ae[i].c);
addedge(ae[i].u,ae[i].v,ae[i].w,0ll);
}
s = 1; t = n; mf = mc = 0ll;
mfmc();
printf("%lld ",mf);
n++; addedge(n-1,n,p,0); t = n;
for(int i=1; i<=m; i++)
{
addedge(ae[i].u,ae[i].v,INF,ae[i].c);
}
mf = mc = 0ll;
mfmc();
printf("%lld\n",mc);
return 0;
}
最新文章
- DG gap sequence修复一例
- Jsp的九个内置对象
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层
- [Tool] PowerDesigner
- nginx服务器安装与启动
- cat >; 命令也可以创建文档
- Lipo Error!! can't open input file
- [每日一题] OCP1z0-047 :2013-08-12 view视图的描述哪些是正确的?
- Java 通过 BufferReader 实现 文件 写入读取 示例
- TFS2010升级至TFS2013完全指南
- vue2 watch引用类型 失败原因
- MyBatis动态SQL小结
- 深入浅出Java MVC(Model View Controller) ---- (JSP + servlet + javabean实例)
- Aizu - 2249 Road Construction
- Docker 后台进程参数-------更改Docker运行根目录的方法
- Mysql修改时间的年月日,时分秒保持不变语句
- 【转】MIUI8以及ViVO X9上在Android Studio运行出错集及其解决方案
- 10分钟教你用Python实现微信自动回复
- 简单说明hadoop集群运行三种模式和配置文件
- WebService 之CXF(rs)之HelloWorld
热门文章
- Git-flow 一个简单高效的Git工作流
- STM32:片上Flash 操作
- SSM整合配置错误记录
- bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路【贪心】
- HTML5常用知识点
- 【懒人专用系列】Xind2TestCase的初步探坑
- 区间DP UVA 11584 Partitioning by Palindromes
- OpenCV2.4.9 + VS2012 + win10 配置
- kali中的APT软件包处理工具(apt-get)、Debian软件包管理器(dpkg)、源代码压缩和Nessus安装实用指南
- 计算机二级C语言冲刺笔记。