【BZOJ3876】 [Ahoi2014]支线剧情
Description
Input
Output
输出一行包含一个整数,表示JYY看完所有支线剧情所需要的最少时间。
Sample Input
2 2 1 3 2
2 4 3 5 4
2 5 5 6 6
0
0
0
Sample Output
HINT
JYY需要重新开始3次游戏,加上一开始的一次游戏,4次游戏的进程是
Source
Solution
有上下界费用流。
构图和上下界的网络流类似,不过从新建源到当前节点的费用要设成本来流进来的费用。
Code
#include <cstdio>
#include <algorithm>
#include <cstring> #define R register
#define maxn 310
#define maxm 23333
#define inf 0x7fffffff
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
struct Edge {
Edge *next, *rev;
int from, to, cap, cost;
} *prev[maxn], *last[maxn], e[maxm], *ecnt = e;
inline void link(R int a, R int b, R int w, R int c)
{
// printf("%d %d %d %d\n", a, b, w, c);
*++ecnt = (Edge) {last[a], ecnt + , a, b, w, c}; last[a] = ecnt;
*++ecnt = (Edge) {last[b], ecnt - , b, a, , -c}; last[b] = ecnt;
}
int s, t, dis[maxn], q[maxn << ], ans, deg[maxn], cs[maxn];
bool inq[maxn];
inline bool spfa()
{
for (R int i = ; i <= t; ++i) dis[i] = inf;
R int head = , tail = ; dis[q[] = s] = ;
while (head < tail)
{
R int now = q[++head]; inq[now] = ;
for (R Edge *iter = last[now]; iter; iter = iter -> next)
if (iter -> cap && dis[iter -> to] > dis[now] + iter -> cost)
{
dis[iter -> to] = dis[now] + iter -> cost;
prev[iter -> to] = iter;
!inq[iter -> to] ? inq[q[++tail] = iter -> to] = : ;
}
}
return dis[t] != inf;
}
inline void mcmf()
{
R int x = inf;
for (R Edge *iter = prev[t]; iter; iter = prev[iter -> from]) cmin(x, iter -> cap);
for (R Edge *iter = prev[t]; iter; iter = prev[iter -> from])
{
iter -> cap -= x;
iter -> rev -> cap += x;
ans += x * iter -> cost;
}
}
int main()
{
R int n; scanf("%d", &n);
s = n + ; t = n + ;
for (R int i = ; i <= n; ++i)
{
R int ni; scanf("%d", &ni);
for (R int j = ; j <= ni; ++j)
{
R int b, c; scanf("%d%d", &b, &c);
link(i, b, inf, c);
link(s, b, , c);
}
if (ni) link(i, t, ni, );
if (i != ) link(i, , inf, );
}
while (spfa()) mcmf();
printf("%d\n", ans);
return ;
}
【BZOJ3876】 [Ahoi2014]支线剧情的更多相关文章
- bzoj3876: [Ahoi2014]支线剧情
神犇题解:http://blog.csdn.net/popoqqq/article/details/43024221 题意:给定一个DAG,1为起始点,任意一个点可以直接回到1,每条边有经过代价,求一 ...
- [bzoj3876][AHOI2014]支线剧情——上下界费用流
题目 传送门 题解 建立s和t,然后s向1连下限0上限inf费用0的边,除1外所有节点向t连下限0上限inf费用0的边,对于每条边下限为1上限为inf费用为经过费用,然后我们只有做上下界网络流构出新图 ...
- 【BZOJ3876】[Ahoi2014]支线剧情 有上下界费用流
[BZOJ3876][Ahoi2014]支线剧情 Description [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩 ...
- 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 821 Solved: 502[Submit][Status ...
- C++之路进阶——bzoj3876(支线剧情)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser hyxzc Logout 捐赠本站 Notice:由于本OJ建立在 ...
- bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 484 Solved: 296[Submit][Status ...
- BZOJ 3876: [Ahoi2014]支线剧情 [上下界费用流]
3876: [Ahoi2014]支线剧情 题意:每次只能从1开始,每条边至少经过一次,有边权,求最小花费 裸上下界费用流...每条边下界为1就行了 注意要加上下界*边权 #include <io ...
- BZOJ 3876: [Ahoi2014]支线剧情 带下界的费用流
3876: [Ahoi2014]支线剧情 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3876 Description [故事背景] 宅 ...
- [Ahoi2014]支线剧情[无源汇有下界最小费用可行流]
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1538 Solved: 940[Submit][Statu ...
随机推荐
- Akka系列(九):Akka分布式之Akka Remote
前言.... Akka作为一个天生用于构建分布式应用的工具,当然提供了用于分布式组件即Akka Remote,那么我们就来看看如何用Akka Remote以及Akka Serialization来构建 ...
- excel常用公式--数据清洗类
trim:去除单元格两端的空格. concat/&:连接单元格内的内容. mid: 提取字符串中间的字符串. left: 提取字符串左边的字符串. right: 提取字符串右边的字符串. ...
- Elasticsearch-索引新数据(创建索引、添加数据)
ES-索引新数据 0.通过mapping映射新建索引 CURL -XPOST 'localhost:9200/test/index?pretty' -d '{ "mappings" ...
- Websocket --(2)实现
首先声明,本篇博文参考文章 https://blog.csdn.net/jack_eusong/article/details/79064081 主要在于理解和自己动手搭建环境,自己搭建的过程中会发生 ...
- redis 小结 一
1.redis 是什么? 它是一个key-value存储系统,也被称为数据结构服务器,它的值是字符串(String),哈希(Hash),列表(list),集合(sets)和有序集合(sorted se ...
- 在mac上配置cocos2d-x开发环境
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012282115/article/details/24374539 一.首先下载cocos2d-x ...
- C语言数据类型关键字
最初 K&R 给出的关键字 C90 标准添加的关键字 C99 标准添加的关键字 int signed _Bool long void _Complex short _Imaginary u ...
- 剑指offer-6:数值整数次方
一.题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.解题思想 分类讨论,充分考虑每种可能. exponent :0,1,& ...
- 常用插件html
1.上传模板,插件 https://github.com/kartik-v/bootstrap-fileinput 2.
- linux centos中安装flash player
本机为centos 7.0 64 1.用火狐浏览器随便打开一个视频网站,找到一个视频点进去,网站会提示未安装flash player.点击进去到adobe官网.2.下载flash player插件,可 ...