_bzoj1061 [Noi2008]志愿者招募【最小费用最大流】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1061
尽管不是mcmf的裸题,但还是保存一下模版叭~
很好的一道建模的题,把变量间的加加减减等效成网络中的流入流量与流出流量,再带上个权,求个最小费用就好,详细题解间此:https://www.byvoid.com/blog/noi-2008-employee/
#include <cstdio>
#include <cstring>
#include <algorithm> const int maxn = 1010, maxm = 10005, maxe = 100000;
const long long inf = 0x3c3c3c3c3c3c3c3cLL; int n, m, a[maxn], s[maxm], t[maxm], cost[maxm], S, T;
int head[maxn], to[maxe], next[maxe], from[maxe], lb;
long long d[maxn], c[maxn], w[maxe], flow[maxe], cap[maxe];
int p[maxn], que[maxn], head_, tail, h;
bool inq[maxn]; inline bool spfa(long long & co) {
memset(d, 0x3c, sizeof d);
head_ = tail = 0;
memset(inq, 0, sizeof inq);
que[tail++] = S;
inq[S] = 1;
c[S] = inf;
d[S] = 0;
while (head_ != tail) {
h = que[head_++];
inq[h] = 0;
if (head_ == T + 3) {
head_ = 0;
}
for (int j = head[h]; j != -1; j = next[j]) {
if (cap[j] > flow[j] && d[to[j]] > d[h] + w[j]) {
d[to[j]] = d[h] + w[j];
c[to[j]] = std::min(c[h], cap[j] - flow[j]);
p[to[j]] = j;
if (!inq[to[j]]) {
que[tail++] = to[j];
inq[to[j]] = 1;
if (tail == T + 3) {
tail = 0;
}
}
}
}
}
if (d[T] == inf) {
return false;
}
co += d[T] * c[T];
for (int i = T; i != S; i = from[p[i]]) {
flow[p[i]] += c[T];
flow[p[i] ^ 1] -= c[T];
}
return true;
}
inline long long mcmf(void) {
long long co = 0;
while (spfa(co));
return co;
} inline void ist(int aa, int ss, long long ww, long long ca) {
to[lb] = ss;
from[lb] = aa;
next[lb] = head[aa];
head[aa] = lb;
w[lb] = ww;
cap[lb] = ca;
++lb; to[lb] = aa;
from[lb] = ss;
next[lb] = head[ss];
head[ss] = lb;
w[lb] = -ww;
cap[lb] = 0;
++lb;
} int main(void) {
//freopen("in.txt", "r", stdin);
memset(head, -1, sizeof head);
memset(next, -1, sizeof next);
scanf("%d%d", &n, &m);
T = n + 2;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
}
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d", s + i, t + i, cost + i);
} for (int i = 1; i <= n + 1; ++i) {
if (a[i - 1] - a[i] >= 0) {
ist(S, i, 0, (long long)(a[i - 1] - a[i]));
}
else {
ist(i, T, 0, (long long)(a[i] - a[i - 1]));
}
}
for (int i = 1; i <= m; ++i) {
ist(t[i] + 1, s[i], (long long)cost[i], inf);
}
for (int i = 1; i <= n; ++i) {
ist(i, i + 1, 0, inf);
}
printf("%lld\n", mcmf());
return 0;
}
_bzoj1061 [Noi2008]志愿者招募【最小费用最大流】的更多相关文章
- bzoj 1061 志愿者招募(最小费用最大流)
[Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3792 Solved: 2314[Submit][Status][Di ...
- 【BZOJ】1061: [Noi2008]志愿者招募(费用流+数学)
http://www.lydsy.com/JudgeOnline/problem.php?id=1061 好神的一题! 学会了一种建模方式: 当方程组内的任意变量都在其中两个方程出现且一正一负,可以建 ...
- BZOJ 1061 [Noi2008]志愿者招募(费用流)
题目描述 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完成,其中第i ...
- NOI2008 志愿者招募 (费用流)
题面 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完成,其中第i 天至 ...
- [BZOJ1061][Noi2008]志愿者招募 线性规划+费用流
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1061 根据题意列方程,然后用网络流解线性规划. 题解直接贴ByVoid的吧,太神了:htt ...
- BZOJ-1061 志愿者招募 线性规划转最小费用最大流+数学模型 建模
本来一眼建模,以为傻逼题,然后发现自己傻逼...根本没想到神奇的数学模型..... 1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 ...
- BZOJ 1061: [Noi2008]志愿者招募 费用流
1061: [Noi2008]志愿者招募 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1061 Description 申奥成功后,布布 ...
- 【费用流】NOI2008志愿者招募
1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 5171 Solved: 3089[Submit][Stat ...
- BZOJ 1061 志愿者招募(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1061 题意:申奥成功后,布布经过不懈努力,终于 成为奥组委下属公司人力资源部门的主管.布 ...
随机推荐
- 对CSS尺寸单位'em'的长期误解
一直以来认为'em'是相对于父元素的字体大小. 直到今天学习移动WEB开发,重新复习css的尺寸大小时,惊奇发现:对em深深的误解了!!! 在CSS官网对em的解释实例是: a. h1{line-he ...
- 报错: The type ByteInputStream is not accessible due to restriction on required library
报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...
- 什么是WPF? 秒懂 !
一開始听到WPF.认为非常陌生.在百度百科等地方看完简单介绍之后.感觉更深奥.各种不懂啊! 在简单做了几个页面之后,发现.原来如此! So Easy 但又So Magic. 为什么说它简单?由于它简直 ...
- 理解Android ANR的触发原理(转)
一.概述 ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过 ...
- HDU1542Atlantis(扫描线)
HDU1542Atlantis(扫描线) 题目链接 题目大意:给你n个覆盖矩形,问最后覆盖的面积. 解题思路:将每一个矩形拆成两条线段,一条是+1的,还有一条是减1的.然后扫描先从上往下扫描,碰到加1 ...
- SQL yog过期后教你怎么让他不过期
打开注册表 安装sqlyog后,进入注册表编辑器,进入\HEYK_CURRENT_USER\Software\,找到以{}括起来的那项直接干掉! 1, regedit 2,修改 3,
- MYSQL将时间格式化
SELECT *,DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y/%m/%d %H:%i:%s") AS dateFormat FROM `I ...
- MaterialImageView
https://github.com/zhaozhentao/MaterialImageView
- hibernate could not resolve property
映射文件字段不匹配 这个异常一般是因为映射文件造成的: 1.hibernate.config.xml中添加的映射文件是否正确 2.映射文件中的类是否正确指定. 3.映射文件映射的属性名是否跟类中的属性 ...
- Lightoj 1025 - The Specials Menu
区间dp /* *********************************************** Author :guanjun Created Time :2016/6/30 23:2 ...