JZOJ 5196. 【NOIP2017提高组模拟7.3】B
5196. 【NOIP2017提高组模拟7.3】B
Description
Input
Output
Sample Input
5 10 40
2 4 80
2 3 57
1 2 16
2 5 49
Sample Output
16
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 300007
#define LL long long
using namespace std;
LL n, S, E, ls[N], tot, Ma[N], Size[N], sum, root, Len ;
LL d[N], ans;
bool v[N];
struct edge
{
LL to, next, w;
}e[N * ];
struct arr
{
LL dis;
int fa;
}a[N]; inline void Add(int x, int y, int w)
{
e[++tot].to = y;
e[tot].w = w;
e[tot].next = ls[x];
ls[x] = tot;
} inline void Getroot(int x, int fa)
{
Ma[x] = , Size[x] = ;
for (int i = ls[x]; i; i = e[i].next)
{
int visit = e[i].to;
if (v[visit] || visit == fa) continue;
Getroot(visit, x);
Size[x] += Size[visit];
Ma[x] = max(Size[visit], Ma[x]);
}
Ma[x] = max(Ma[x], sum - Size[x]);
if (Ma[x] < Ma[root]) root = x;
} inline void Getval(int x, int fa, int zs)
{
a[++Len].dis = d[x];
a[Len].fa = zs;
for (int i = ls[x]; i; i = e[i].next)
{
int visit = e[i].to;
if (v[visit] || visit == fa) continue;
d[visit] = d[x] + e[i].w;
if (!zs) Getval(visit, x, visit);
else Getval(visit, x, zs);
}
} int cmp(arr x, arr y)
{
return x.dis < y.dis;
} inline LL Acu(int x)
{
Len = , d[x] = ;
Getval(x, , );
sort(a + , a + Len + , cmp);
int l = , r = Len;
for (; l < r; )
{
if (a[l].fa == a[r].fa)
{
if (a[l].dis + a[r - ].dis < S) l++;
else r--;
}
else
{
if (a[l].dis + a[r].dis >= S)
{
ans = min(ans,(LL)(a[l].dis + a[r].dis));
r--;
}
else l++;
}
}
} inline void work(int x)
{
Acu(x);
v[x] = ;
for (int i = ls[x]; i; i = e[i].next)
{
int visit = e[i].to;
if (v[visit]) continue;
sum = Size[visit];
root = ;
Getroot(visit, );
work(root);
}
} int main()
{
scanf("%d%d%d", &n, &S, &E);
memset(v, , sizeof(v));
memset(ls, , sizeof(ls));
for (int i = ; i <= n - ; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
Add(u, v, w);
Add(v, u, w);
}
ans = 0x3f3f3f3f;
sum = n;
Ma[] = 0x3f3f3f3f;
Getroot(, );
work(root);
if (ans > E) printf("-1");
else printf("%lld", ans);
}
JZOJ 5196. 【NOIP2017提高组模拟7.3】B的更多相关文章
- [jzoj 5178] [NOIP2017提高组模拟6.28] So many prefix? 解题报告(KMP+DP)
题目链接: https://jzoj.net/senior/#main/show/5178 题目: 题解: 我们定义$f[pos]$表示以位置pos为后缀的字符串对答案的贡献,答案就是$\sum_{i ...
- [jzoj 5177] [NOIP2017提高组模拟6.28] TRAVEL 解题报告 (二分)
题目链接: https://jzoj.net/senior/#main/show/5177 题目: 题解: 首先选出的泡泡怪一定是连续的一段 L,R 然后 L 一定属于虫洞左边界中的某一个 R 也同样 ...
- JZOJ 5197. 【NOIP2017提高组模拟7.3】C
5197. [NOIP2017提高组模拟7.3]C Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...
- JZOJ 5195. 【NOIP2017提高组模拟7.3】A
5195. [NOIP2017提高组模拟7.3]A Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...
- JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift
5184. [NOIP2017提高组模拟6.29]Gift (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- JZOJ 5185. 【NOIP2017提高组模拟6.30】tty's sequence
5185. [NOIP2017提高组模拟6.30]tty's sequence (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB ...
- NOIP2017提高组 模拟赛15(总结)
NOIP2017提高组 模拟赛15(总结) 第一题 讨厌整除的小明 [题目描述] 小明作为一个数学迷,总会出于数字的一些性质喜欢上某个数字,然而当他喜欢数字k的时候,却十分讨厌那些能够整除k而比k小的 ...
- NOIP2017提高组 模拟赛13(总结)
NOIP2017提高组 模拟赛13(总结) 第一题 函数 [题目描述] [输入格式] 三个整数. 1≤t<10^9+7,2≤l≤r≤5*10^6 [输出格式] 一个整数. [输出样例] 2 2 ...
- NOIP2017提高组模拟赛 10 (总结)
NOIP2017提高组模拟赛 10 (总结) 第一题 机密信息 FJ有个很奇怪的习惯,他把他所有的机密信息都存放在一个叫机密盘的磁盘分区里,然而这个机密盘中却没有一个文件,那他是怎么存放信息呢?聪明的 ...
随机推荐
- tcpdump安装配置及抓包分析
http://blog.csdn.net/e421083458/article/details/23963189 cpdump安装配置及抓包分析 预装软件:[plain] view plain cop ...
- 3 - Selenium元素定位和操作
3.1定位 <button id="gbqfba" aria-label="Google Search" name="btnK" cl ...
- Kotlin基础知识
1. 改进点/基础 //安全判空 val length = text?.length; //类型转换 if (object is Car) { var car = object as Ca } //操 ...
- HDU 5501——The Highest Mark——————【贪心+dp】
The Highest Mark Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- css相关知识
display: block; "块级元素". display: inline; "行内元素". display: none; "在不删除元素的情况下 ...
- java实现定时任务(Quartz)
java中实现定时任务执行某一业务.具体操作如下: 1.定义初始化任务 2.任务业务操作 3.定义初始化方法 4.在web.xml中注册启动 5.定义具体执行时间 6.quartz定时任务时间设置 q ...
- Regexp:常用的几个正则表达式
1.isEmail /** * * @desc 判断是否为邮箱地址 * @param {String} str * @return {Boolean} */ function isEmail(str) ...
- 关于web开发中订单自动超时和自动收货的简单方法(window server)
最近做一个订单自动超时和自动收货的功能,因为以前是用的mysql 存储过程和定时器来完成,这次的业务逻辑相对复杂用以前的方式就不太合适,本来是准备使用定时执行php脚本来实现的,后来发现业务逻辑中使用 ...
- SQL Server DBA SQL
. 监控事例的等待 ,,)) "Prev", ,,)) "Curr",count(*) "Tot" from v$session_Wait ...
- pg中的非varchar类型的模糊搜索
模糊搜索,bay字段是 numeric 类型的(如果是 varchar 类型的用常规的即可) 1,SELECT * FROM s_view_monitor_result WHERE bay ~~ CA ...