【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 ...
随机推荐
- Office_Word使用技巧大全(超全)
目录 不收藏不行的 word 使用技巧大全 三招去掉页眉那条横线 批量转换全角字符为半角字符 快速打开最后编辑的文档 格式刷的使用 删除网上 下载 资料的换行符(象这种 "↓" ) ...
- spring boot-7.日志系统
日志系统分为两部分,一部分是日志抽象层,一部分是日志实现层.常见的日志抽象层JCL,SLF4J,JBoss-Logging,日志实现层有logback,log4j,log4j2,JUL.日志抽象层的功 ...
- Atomic实现原子性源码分析:CAS(比较并交换)、Unsafe类
1.CAS: 比较并交换(Compare And Swap),是Unsafe类中的一条CPU系统原语,原语的执行必须是连续的,在执行过程中不允许被中断,即CAS是一条CPU的原子指令,不会造成数据不一 ...
- Spring中pom文件所需节点
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- Java作业 题目:16版.情人节送玫瑰花
题目:16版.情人节送玫瑰花 题干: 1.实验要求 本实验要求:以情人节送花为业务背景,体验自定义异常以及异常处理机制. 1-1. 业务说明: 1-1.1. 本实验以情人节送花为业务背景,女方提出送花 ...
- 缓存---LRU算法实现
2.LRU 以下是基于双向链表+HashMap的LRU算法实现,对算法的解释如下: 设置一个map存放对应的键和值,同时设置一个双向链表,来保存最近最久未使用的关系,如果访问一个键,键存在于m ...
- 在.NET Core 3.0中发布单个Exe文件(PublishSingleFile)
原文:在.NET Core 3.0中发布单个Exe文件(PublishSingleFile) 假设我有一个简单的" Hello World"控制台应用程序,我想发送给朋友来运行.朋 ...
- Linux常用命令及Shell的简单介绍
一.linux命令 1.查看指令的参数搭配: man 指令名称 2.基础指令 ls 列出当前目录下的所有文档的名称(文档指的是文件和文件夹) 常用参数搭配: ls -l 列出文档详细信息 l ...
- Coco56公众号关键字索引
目录 1. 本文地址 2. 公众号介绍 3. 关键词及含义 1. 本文地址 博客园:https://www.cnblogs.com/coco56/p/11182421.html 简书:https:// ...
- 数据库备份及SQL脚本导入
数据库备份及SQL脚本导入 数据导出 su - oracle exp 数据库用户名/数据库密码@ORCL file=20190905.dmp full=y SQL脚本导入 首先导入前查看Oracle用 ...