传送门: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. ArcEngine读取ShapeFile时,出现乱码的解决方案

    ArcEngine读取ShapeFile时,如果用LicenseControl的话,字段中含有汉字时可以正常使用,当使用LicenseInitializer进行初始化时,读取含有汉字的字段时,就会出现 ...

  2. zerorpc使用时报错:No handlers could be found for logger "zerorpc.channel"

    问题如题:安装方法参考 http://www.cnblogs.com/shengulong/p/7887586.html ,安装完后,使用时出现如题的错误 解决办法: 1.zerorpc本身依赖很多三 ...

  3. 我被C++开发欺辱的岁月

    前言 人被压迫了,为什么不斗争?——鲁迅 作为一个C#开发者,我经历了,也见证了很多同行饱受C++开发的歧视和欺辱. 而且,这种行为,现在依然持续的发生在C#开发者的身上,就目前为止,绝大部分C#开发 ...

  4. 我的Android Studio 优化之路

    改动keymap 改动经常使用的快捷键 代码补全(Eclipse: ALT+/) Android Studio中默认用的是Ctrl+Space, 这跟输入法切换冲突.找到Keymap->Main ...

  5. Windows 上通过本地搭建 Jekyll环境

    一 准备Ruby环境 1 我们首先须要安装Ruby.从站点下载Ruby 上下载Ruby最新版和对应的DevKit. 我下载的是Ruby 2.1.4 (x64)和DevKit-mingw64-6 .注意 ...

  6. 将Python打印的内容进行高亮的输出

    将打印的内容进行高亮的显示 内容: 格式: echo "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: "\033[41;36m something he ...

  7. ImageViewCoverflow

    https://github.com/Bertlk/ImageViewCoverflow https://github.com/dolphinwang/ImageCoverFlow http://ww ...

  8. 怎么将本地文件上传到远程git仓库

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“ ...

  9. 【日常学习】codevs1287 矩阵乘法题解

    转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看. 先上题目 题目描写叙述 Description 小明近期在为线性代数而头疼,线性代数确实非 ...

  10. 关于java及多线程

    http://www.w3cschool.cc/java/java-multithreading.html