传送门: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]志愿者招募【最小费用最大流】的更多相关文章

  1. bzoj 1061 志愿者招募(最小费用最大流)

    [Noi2008]志愿者招募 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3792  Solved: 2314[Submit][Status][Di ...

  2. 【BZOJ】1061: [Noi2008]志愿者招募(费用流+数学)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 好神的一题! 学会了一种建模方式: 当方程组内的任意变量都在其中两个方程出现且一正一负,可以建 ...

  3. BZOJ 1061 [Noi2008]志愿者招募(费用流)

    题目描述 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完成,其中第i ...

  4. NOI2008 志愿者招募 (费用流)

    题面 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完成,其中第i 天至 ...

  5. [BZOJ1061][Noi2008]志愿者招募 线性规划+费用流

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1061 根据题意列方程,然后用网络流解线性规划. 题解直接贴ByVoid的吧,太神了:htt ...

  6. BZOJ-1061 志愿者招募 线性规划转最小费用最大流+数学模型 建模

    本来一眼建模,以为傻逼题,然后发现自己傻逼...根本没想到神奇的数学模型..... 1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 ...

  7. BZOJ 1061: [Noi2008]志愿者招募 费用流

    1061: [Noi2008]志愿者招募 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1061 Description 申奥成功后,布布 ...

  8. 【费用流】NOI2008志愿者招募

    1061: [Noi2008]志愿者招募 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 5171  Solved: 3089[Submit][Stat ...

  9. BZOJ 1061 志愿者招募(最小费用最大流)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1061 题意:申奥成功后,布布经过不懈努力,终于 成为奥组委下属公司人力资源部门的主管.布 ...

随机推荐

  1. ZOJ 2706 Thermal Death of the Universe (线段树)

    题目链接:ZOJ 2706 Thermal Death of the Universe (线段树) 题意:n个数.m个操作. 每一个操作(a,b)表示(a,b)全部值更新为这个区间的平均数:1.当前的 ...

  2. Linux环境搭建:1. 安装VMware

    我家淘宝店,主要协助同学做毕业设计:https://shop104550034.taobao.com/?spm=2013.1.1000126.d21.pPCzDZ 1. 下载VMware 能够到我的3 ...

  3. WPF 创建二维码

    1.在http://zxingnet.codeplex.com/网站上下载ZXing .Net的第三方库 2.新建一个WPFproject 3.引入zxing.dll 4.加入引用空间 using Z ...

  4. Mysql Solution - Timeout error occurred trying to stop MySQL Daemon. Stopping MySQL: [FAILED] -

    错误例如以下: Timeout error occurred trying to stop MySQL Daemon. Stopping mysqld:                         ...

  5. 2016/05/25 empty() 与 isset()的区别

    对于初学php的人来说,empty()和和isset()用法的区别是很难搞清楚的,他们的用法的差别不仔细去琢磨的话确实很难弄清楚. 先说一下他们的共同点: 都可以判定一个变量是否为空: 都返回bool ...

  6. HDU1007(求近期两个点之间的距离)

    一年前学长讲这题的时候,没听懂.自己搜解题报告也看不懂,放了一年. 现在对分治和递归把握的比一年前更加熟悉,这题也就攻克了. 题意:给你一堆点让你求近期两点之间距离的一半,假设用暴力的话O(n*n)明 ...

  7. 【转】 Android Studio --“Cannot resolve symbol” 解决办法

    Android Studio 无法识别同一个 package 里的其他类,将其显示为红色,但是 compile 没有问题.鼠标放上去后显示 “Cannot resolve symbol XXX”,重启 ...

  8. inexact rename detection was skipped due to too many files

    https://stackoverflow.com/a/28064699 error: add_cacheinfo failed to refresh for path 'LISA.Kentico.U ...

  9. YTU 2894: G--我要去内蒙古大草原

    2894: G--我要去内蒙古大草原 时间限制: 1 Sec  内存限制: 128 MB 提交: 162  解决: 8 题目描述 春天到了,小明想要从烟台开车去内蒙古大草原放松一下,这两地的距离是14 ...

  10. YTU 2845: 编程题AB-卡片游戏

    2845: 编程题AB-卡片游戏 时间限制: 1 Sec  内存限制: 128 MB 提交: 30  解决: 13 题目描述 小明对数字的序列产生了兴趣: 现有许多张不同的数字卡片,用这若干张卡片能排 ...