【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 ...
随机推荐
- [转帖]RSA算法与DSA算法的区别
RSA算法与DSA算法的区别 https://cloud.tencent.com/developer/news/254061 文章来源:企鹅号 - SuperFullStack 本文译自:StackE ...
- eclipse中svn的使用
1.在eclipse中添加SVN插件或者说直接利用eclipse软件中的help-install项进行网站在线下载也可 2.安装好SVN之后, 2.1.从SVN检出项目到本地 右击鼠标-选import ...
- sql中循环的存储过程
), a2 bigint, a3 bigint) returns void as $$declare ii integer; begin II:; FOR ii IN a2..a3 LOOP INSE ...
- 第一课 初识Linux(一)
Linux起源 创始人:李纳斯.托瓦兹 Linux简介: Linux是一套免费使用和自由传播的类UNIX操作系统:是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.Lin ...
- JSP的9大内置对象和4打作用域对象
一.9大内置对象 二.4大内置作用域对象
- HTML-简单动画
简单动画 (1)简单动画通常称之为“过渡transition” Transition-property:需要过渡的属性,但是并非所有的属性都支持过渡. Transition-duration:过渡的时 ...
- export ,export default 和 import 区别以及用法
首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...
- 69. Sqrt(x) (JAVA)
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- Struts2.5以上版本There is no Action mapped for namespace [/] and action name [userAction_login] associated with context path []
分析:Struts2在2.5版本后添加strict-method-invocation(严格方法访问),默认为true,不能使用动态方法调用功能,故需设为false struts.xml设置如下: & ...
- springboot学习1
gradle环境配置 https://www.w3cschool.cn/gradle/ctgm1htw.html Spring profile 多环境配置管理 参考:https://www.cnblo ...