【洛谷 P4016】 负载平衡问题(费用流)
题目链接
环形均分纸牌,既然是网络流23题的那就用网络流做把。
套路拆点。
供需平衡。
源点向大于平均数的点的入点连流量为这个数减去平均数的差,费用为0的边,表示需要移走这么多。
小于平均数的点的出点向汇点连流量为平均数减去这个数的差,费用为0的边,表示可以容纳这么多。
然后相邻入点两两连边,入点再向相邻的出点连边,都是流量1费用0。
然后最小费用就是答案。
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 2147483647
using namespace std;
const int MAXN = 333;
const int MAXM = 20010;
struct Edge{
int from, next, to, rest, cost;
}e[MAXM];
int head[MAXN], num = 1, n, m, k;
inline void Add(int from, int to, int flow, int cost){
e[++num] = (Edge){from, head[from], to, flow, cost}; head[from] = num;
e[++num] = (Edge){to, head[to], from, 0, -cost}; head[to] = num;
}
int s, t, a[MAXN], now, mincost, sum;
queue <int> q;
int v[MAXN], dis[MAXN], pre[MAXN], flow[MAXN];
int re(){
q.push(s);
memset(dis, 127, sizeof dis);
memset(flow, 0, sizeof flow);
dis[s] = 0; pre[t] = 0; flow[s] = INF;
while(q.size()){
now = q.front(); q.pop(); v[now] = 0;
for(int i = head[now]; i; i = e[i].next)
if(e[i].rest && dis[e[i].to] > dis[now] + e[i].cost){
dis[e[i].to] = dis[now] + e[i].cost;
pre[e[i].to] = i; flow[e[i].to] = min(flow[now], e[i].rest);
if(!v[e[i].to]) v[e[i].to] = 1, q.push(e[i].to);
}
}
return pre[t];
}
int main(){
scanf("%d", &n); s = 321; t = 322;
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]), sum += a[i];
sum /= n;
for(int i = 1; i <= n; ++i)
if(a[i] > sum)
Add(s, i, a[i] - sum, 0);
else if(a[i] < sum)
Add(i + n, t, sum - a[i], 0);
if(n == 2) Add(1, 2, INF, 1), Add(2, 1, INF, 1); else
for(int i = 1; i <= n; ++i){
if(i == 1) Add(1, 2, INF, 1), Add(1, n, INF, 1), Add(1, 2 + n, INF, 1), Add(1, n + n, INF, 1);
else if(i == n) Add(n, n - 1, INF, 1), Add(n, 1, INF, 1), Add(n, n + n - 1, INF, 1), Add(n, n + 1, INF, 1);
else Add(i, i + 1, INF, 1), Add(i, i - 1, INF, 1), Add(i, i + 1 + n, INF, 1), Add(i, i - 1 + n, INF, 1);
}
while(re()){
now = pre[t];
while(now){
e[now].rest -= flow[t];
e[now ^ 1].rest += flow[t];
mincost += e[now].cost * flow[t];
now = pre[e[now].from];
}
}
printf("%d\n", mincost);
return 0;
}
【洛谷 P4016】 负载平衡问题(费用流)的更多相关文章
- 洛谷P4016 负载平衡问题 费用流
这道题还是很好的. 考察了选手对网络流的理解. 首先,任意两个相邻点之间的运货量时没有限制的. 我们可以将相邻点之间的流量建为无限大,单位费用设为 1,代表运输一个货物需耗费一个代价. 由于题目要求最 ...
- 洛谷 P4016负载平衡问题【费用流】题解+AC代码
洛谷 P4016负载平衡问题 P4014 分配问题[费用流]题解+AC代码 负载平衡问题 题目描述 GG 公司有n个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n ...
- (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币
bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...
- 洛谷P4016负载平衡
题目 负载平衡问题是一个比较经典的网络流问题,但是该问题还有一个数学贪心法. 所以做这个题前,其实可以做一下均分纸牌问题. 均分纸牌问题 均分纸牌问题可以说是作为贪心的入门题. 做法 首先我们应当把原 ...
- 洛谷P4016 负载平衡问题(费用流)
传送门 嗯……完全不会……不过题解似乎讲的挺清楚…… 考虑一下,每一个仓库最终肯定都是平均数,所以数量大于平均数的可以往外运,小于平均数的要从别的地方运进来 考虑建一个超级源$S$和超级汇$T$,并把 ...
- 洛谷 P4016 负载平衡问题 【最小费用最大流】
求出平均数sum,对于大于sum的点连接(s,i,a[i]-sum,0),表示这个点可以流出多余的部分,对于小于sum的点连接(i,t,sum-a[i],0)表示这个点可以接受少的部分,然后每个点向相 ...
- 洛谷P4016 负载平衡问题(最小费用最大流)
题目描述 GG 公司有 nn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 nn 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格 ...
- 洛谷 [P4016] 负载平衡问题
贪心做法 第一眼看见觉得和均分纸牌差不多,然而因为这是环形的,并不能用均分纸牌的方法做,但是均分纸牌的思想仍然适用 首先我们假设平均数为sum1. 那么对于第1个人,我们假设他给第N个人K个糖果, 第 ...
- 洛谷P4016 负载平衡问题
题目描述 G 公司有 n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格式: ...
- 『题解』洛谷P4016 负载平衡问题
title: categories: tags: - mathjax: true --- Problem Portal Portal1:Luogu Portal2: LibreOJ Descripti ...
随机推荐
- linux虚拟机发邮件给163邮件
配置/etc/mail.rc文件 set from=xxxxxxxx@163.com smtp=smtp.163.com set smtp-auth-user=yinhuanyi_cn@163.com ...
- 【Leetcode】771. Jewels and Stones
(找了leetcode上最简单的一个题来找一下存在感) You're given strings J representing the types of stones that are jewels, ...
- Java MD5加密类
/************************************************* md5 类实现了RSA Data Security, Inc.在提交给IETF 的RFC1321中 ...
- 【Linux】- 简明Vim练习攻略
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- python配置文件读取
在代码实现的过程中,我们经常选择将一些固定的参数值写入到一个单独的配置文件中.在python中读取配置文件官方提供了configParser方法. 主要有如下方法(找官文): (这家伙很懒,直接复 ...
- TP中循环遍历
循环遍历(重点) 在ThinkPHP中系统提供了2个标签来实现数组在模版中的遍历: volist标签.foreach标签. Volist语法格式: Foreach语法格式: 从上述的语法格式发现vol ...
- HDU4669_Mutiples on a circle
题目的意思是给你一些数字a[i](首位相连),现在要你选出一些连续的数字连续的每一位单独地作为一个数位.现在问你有多少种选择的方式使得选出的数字为k的一个倍数. 其实题目是很简单的.由于k不大(200 ...
- 【Java】数组升序和降序
int[] x={1,6,4,8,6,9,12,32,76,34,23}; 升序: Arrays.sort(x); 降序: resort(x); public int[] resort(int[] n ...
- BZOJ4104:[Thu Summer Camp 2015]解密运算——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4104 对于一个长度为N的字符串,我们在字符串的末尾添加一个特殊的字符".".之 ...
- Splitter Control for Dialog
原文链接地址:https://www.codeproject.com/Articles/595602/Splitter-Control-for-Dialog Introduction Yes, tha ...