洛谷.4015.运输问题(SPFA费用流)
嗯。。水题
洛谷这网络流二十四题的难度评价真神奇。。
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=206,M=15000,INF=0x3f3f3f3f;
int n,m,src,des,A[N],B[N],C[N][N],dis[N],pre[N];
int Enum,H[N],nxt[M<<1],fr[M<<1],to[M<<1],cost[M<<1],cap[M<<1];
bool inq[N];
std::queue<int> q;
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
inline void AddEdge(int u,int v,int w,int c)
{
to[++Enum]=v, fr[Enum]=u, nxt[Enum]=H[u], H[u]=Enum, cap[Enum]=w, cost[Enum]=c;
to[++Enum]=u, fr[Enum]=v, nxt[Enum]=H[v], H[v]=Enum, cap[Enum]=0, cost[Enum]=-c;
}
bool SPFA()
{
memset(dis,0x3f,sizeof dis);
dis[src]=0, q.push(src);
while(!q.empty())
{
int x=q.front();q.pop();
inq[x]=0;
for(int i=H[x]; i; i=nxt[i])
if(cap[i] && dis[to[i]]>dis[x]+cost[i])
{
dis[to[i]]=dis[x]+cost[i], pre[to[i]]=i;
if(!inq[to[i]]) q.push(to[i]),inq[to[i]]=1;
}
}
return dis[des]<INF;
}
int MCMF()
{
int res=0,mn=INF;
for(int i=des; i!=src; i=fr[pre[i]])
mn=std::min(cap[pre[i]],mn);
for(int i=des; i!=src; i=fr[pre[i]])
cap[pre[i]]-=mn,cap[pre[i]^1]+=mn,res+=cost[pre[i]]*mn;
return res;
}
int main()
{
n=read(),m=read();
Enum=1, src=0, des=n+m+1;
for(int i=1; i<=n; ++i) AddEdge(src,i,A[i]=read(),0);
for(int i=1; i<=m; ++i) AddEdge(i+n,des,B[i]=read(),0);
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j)
AddEdge(i,j+n,INF,C[i][j]=read());
int rmin=0,rmax=0;
while(SPFA()) rmin+=MCMF();
memset(H,0,sizeof H);
Enum=1;
for(int i=1; i<=n; ++i) AddEdge(src,i,A[i],0);
for(int i=1; i<=m; ++i) AddEdge(i+n,des,B[i],0);
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j)
AddEdge(i,j+n,INF,-C[i][j]);
while(SPFA()) rmax+=MCMF();
printf("%d\n%d",rmin,-rmax);
return 0;
}
洛谷.4015.运输问题(SPFA费用流)的更多相关文章
- 洛谷P4015 运输问题(费用流)
传送门 源点向仓库连费用$0$,流量为储量的边,商店向汇点连费用$0$,流量为需求的边,然后仓库向商店连流量$inf$,费用对应的边,跑个费用流即可 //minamoto #include<io ...
- 洛谷 - P4452 - 航班安排 - 费用流
https://www.luogu.org/problemnew/show/P4452 又一道看题解的费用流. 注意时间也影响节点,像题解那样建边就少很多了. #include<bits/std ...
- 洛谷P4014 分配问题(费用流)
传送门 可以把原图看做一个二分图,人在左边,任务在右边,求一个带权的最大和最小完美匹配 然而我并不会二分图做法,所以只好直接用费用流套进去,求一个最小费用最大流和最大费用最大流即可 //minamot ...
- 洛谷P4015 运输问题(费用流)
题目描述 WW 公司有 mm 个仓库和 nn 个零售商店.第 ii 个仓库有 a_iai 个单位的货物:第 jj 个零售商店需要 b_jbj 个单位的货物. 货物供需平衡,即\sum\limits ...
- 洛谷.3381.[模板]最小费用最大流(zkw)
题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...
- [洛谷P4015]运输问题
题目大意:有m个仓库和n个商店.第i个仓库有 $a_{i}$ 货物,第j个商店需要$b_{j}$个货物.从第i个仓库运送每单位货物到第j个商店的费用为$c_{i,j}$.求出最小费用和最大费用 题 ...
- 【网络流24题】No. 17 运输问题 (费用流)
[题意] W 公司有 m 个仓库和 n 个零售商店.第 i 个仓库有ai 个单位的货物:第 j 个零售商店需要b j 个单位的货物. 货物供需平衡,即SIGMA(A)=SIGMA(B). 从第 i 个 ...
- 2018.10.14 loj#6011. 「网络流 24 题」运输问题(费用流)
传送门 费用流入门题. 直接按照题意模拟. 把货物的数量当做容量建边. 然后跑一次最小费用流和最大费用流就行了. 代码: #include<bits/stdc++.h> #define N ...
- 【PowerOJ1752&网络流24题】运输问题(费用流)
题意: 思路: [问题分析] 费用流问题. [建模方法] 把所有仓库看做二分图中顶点Xi,所有零售商店看做二分图中顶点Yi,建立附加源S汇T. 1.从S向每个Xi连一条容量为仓库中货物数量ai,费用为 ...
随机推荐
- 【逆向工具】IDA使用3-全局变量、数组、结构体
全局变量 测试代码 全局变量既可以是某对象函数创建,也可以是在本程序任何地方创建.全局变量是可以被本程序所有对象或函数引用.下面这段代码中将int.float.char变量定义在main函数之外. / ...
- 【网络编程4】网络编程基础-ARP响应(ARP欺骗之中间人攻击)
arp欺骗->arp响应 ARP 缓存中毒(ARP欺骗) arp传送原理在于主机发送信息时将包含目标IP地址的ARP请求广播到网络上的所有主机,并接收返回消息,以此确定目标的物理地址:收到返回消 ...
- Linux内核调试 - 一般人儿我都不告诉他(一)【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/04/14/2396821.html 悄悄地进入Linux内核调试(一) 本文基址:http://blo ...
- 使用 Virtual Machine Manager 管理虚拟机
转载自https://www.ibm.com/developerworks/cn/cloud/library/cl-managingvms/ 尽管服务器管理在过去问题重重,但虚拟化管理简化了一些问 ...
- activemq 消息类型
//文本消息 TextMessage textMessage = session.createTextMessage("文本消息"); producer.send(textMess ...
- mtk 无线配置文件生效过程
openwrt 下无线接口的配置文件位于 /etc/config/wirless 中. 启动 /sbin/wifi 脚本后,生效过程如下: (1)通过 uci2dat 工具生成所需要的 .dat文件 ...
- SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4) 1.1 前提条件 1) 已安装Java环境(version:1.7+) 2) 已安装MyS ...
- Go语言规格说明书 之 类型(Types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...
- 输入年月日判断是当年第几天(if判断)
- centos下配置DNS
centos网络配置实例 1,配置DNSvi /etc/resolv.conf加入: 代码如下: nameserver 192.168.0.1 nameserver 8.8.8.8 nameserve ...