【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 ...
随机推荐
- ArrayList和LinkedList的底层代码实现思想
ArrayList是Java众多集合类中的一个,实现List接口,List的父接口是Collection.ArrayList底层的数据结构是线性表中的顺序表,底层是一个长度可以动态增长的数组.数组有很 ...
- 解决 Illegal DefaultValue null for parameter type integer 异常
该异常是由 swagger 引起的 swagger 版本 1.9.2 解决原因:重新导入 swagger-annotations 和 swagger-models 版本 为 1.5.21 pom.xm ...
- 小记---------Elasticsear搭建
Elasticsear搭建 创建用户: useradd elasticsearch passwd elasticsearch 1.解压 tar -zxvf elasticsearch-5.5.2. ...
- 第一次参赛经历:ecfinal总结
刚接到要去参加ec的消息时,还是非常激动的,毕竟第一次参赛就参加如此高水平的编程竞赛(更高水平的比赛就是wf). 教练临时组队,把三个从没在一起打过比赛的三个人组成一队,当时有点担心默契和配合上的问题 ...
- Python 入门之 内置模块 -- random模块
Python 入门之 内置模块 -- random模块 1.random模块 import random # random -- 随机数 (1)选择1-50之间随机的整数 print(random.r ...
- $NOI$ $2019$ 网络同步赛
D2T1考试前测了自己造的“假”极限数据,看了看内存发现是118MB,感觉没问题就交上去了. 赛后用Lemon测了一下:MLE 88->0 对于别的题我已经不想再说什么了. update: 由于 ...
- ivew组件上传图片文件的功能:
解决的问题: 1.使用view的<Upload>组件实现图片文件的上传. 2.<Upload>组件action请求地址无法到自己写的后台. 3.前台base64的图片展示. 4 ...
- java中的集合类详情解析以及集合和数组的区别
数组和链表 数组:所谓数组就是相同数据类型的元素按照一定顺序排列的集合. 它的存储区间是连续的,占用内存严重,所以空间复杂度很大,为o(n),但是数组的二分查找时间复杂度很小为o(1). 特点是大小固 ...
- java 序列化原来如此
上次面试的时候 ,如何实现java 类的序列化,当时感觉这个问题很简单,我的回答是实现serizlizable 接口就好了,可以实现对象的持久化,看了看书,原来这样: public class Ser ...
- Delphi 常量