题目

思路

不想写了,直接使用

没错,关键就在求第 \(k\) 小的路径

上述提到堆的做法,我们可以用 \(STL\) 的优先队列来实现

只不过常数有点大~~~

\(Code\)

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL; const int N = 1e5 + 5;
int n , k , h[N] , tot , cnt1 , cnt2;
LL a[N] , b[N]; struct edge{
int to , nxt , w;
}e[N << 1]; struct node{
int l , r;
LL d;
bool operator < (node c) const {return d > c.d;}
}; priority_queue<node> Q; inline void add(int x , int y , int z)
{
e[++tot].to = y;
e[tot].w = z;
e[tot].nxt = h[x];
h[x] = tot;
} inline void dfs(int x , int fa , int fl , LL D , int ad)
{
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
D = D + 1LL * fl * e[i].w;
if (ad) a[++cnt1] = D;
else b[++cnt2] = -D;
dfs(v , x , -fl , D , ad ^ 1);
D = D - 1LL * fl * e[i].w;
}
} int main()
{
freopen("travel.in" , "r" , stdin);
freopen("travel.out" , "w" , stdout);
scanf("%d%d" , &n , &k);
int u , v , w;
for(register int i = 1; i < n; i++)
{
scanf("%d%d%d" , &u , &v , &w);
add(u , v , w) , add(v , u , w);
}
b[++cnt2] = 0;
dfs(1 , 0 , 1 , 0 , 1);
sort(a + 1 , a + cnt1 + 1) , sort(b + 1 , b + cnt2 + 1);
for(register int i = 1; i <= cnt1; i++) Q.push((node){i , 1 , a[i] + b[1]});
node now;
for(register int i = 1; i <= k; i++)
{
if (Q.size())
{
now = Q.top() , Q.pop();
if (i == k)
{
printf("%lld" , now.d);
return 0;
}
if (now.r + 1 <= cnt2) Q.push((node){now.l , now.r + 1 , a[now.l] + b[now.r + 1]});
}
else{
printf("Stupid Mike");
return 0;
}
}
}

对于此类问题,我们还有一个很经典的做法

二分答案,然后判断路径组合中比这个答案小的能不能达到 \(k\)

后半句可以再套个二分实现

\(Code\)

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL; const int N = 1e5 + 5;
int n , k , h[N] , tot , cnt1 , cnt2;
LL a[N] , b[N]; struct edge{
int to , nxt , w;
}e[N << 1]; inline void add(int x , int y , int z)
{
e[++tot].to = y;
e[tot].w = z;
e[tot].nxt = h[x];
h[x] = tot;
} inline void dfs(int x , int fa , int fl , LL D , int ad)
{
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
D = D + 1LL * fl * e[i].w;
if (ad) a[++cnt1] = D;
else b[++cnt2] = -D;
dfs(v , x , -fl , D , ad ^ 1);
D = D - 1LL * fl * e[i].w;
}
} inline int check(LL m)
{
int l , r , mid , res , sum = 0;
for(register int i = 1; i <= cnt1; i++)
{
l = 1 , r = cnt2 , res = 0;
while (l <= r)
{
mid = (l + r) >> 1;
if (b[mid] + a[i] <= m) res = mid , l = mid + 1;
else r = mid - 1;
}
sum += res;
}
return sum >= k;
} int main()
{
freopen("travel.in" , "r" , stdin);
freopen("travel.out" , "w" , stdout);
scanf("%d%d" , &n , &k);
int u , v , w;
for(register int i = 1; i < n; i++)
{
scanf("%d%d%d" , &u , &v , &w);
add(u , v , w) , add(v , u , w);
}
b[++cnt2] = 0;
dfs(1 , 0 , 1 , 0 , 1);
sort(a + 1 , a + cnt1 + 1) , sort(b + 1 , b + cnt2 + 1);
if ((LL)cnt1 * cnt2 < k)
{
printf("Stupid Mike");
return 0;
}
LL l = a[1] + b[1] , r = a[cnt1] + b[cnt2] , mid , res = l;
while (l <= r)
{
mid = (l + r) >> 1;
if (check(mid)) res = mid , r = mid - 1;
else l = mid + 1;
}
printf("%lld" , res);
}

实际上这两做法各有优劣

如果要求前 \(k\) 的话显然用堆,它的过程本质上就是取出了前 \(k\) 的数

JZOJ 4320. 【NOIP2015模拟11.5】旅行的更多相关文章

  1. [JZOJ 4307] [NOIP2015模拟11.3晚] 喝喝喝 解题报告

    题目链接: http://172.16.0.132/senior/#main/show/4307 题目: 解题报告: 题目询问我们没出现坏对的连续区间个数 我们考虑从左到有枚举右端点$r$,判断$a[ ...

  2. 【NOIP2015模拟11.5】JZOJ8月5日提高组T3 旅行

    [NOIP2015模拟11.5]JZOJ8月5日提高组T3 旅行 题目 若不存在第\(k\)短路径时,输出"Stupid Mike" 题解 题意 给出一个有\(n\)个点的树 问这 ...

  3. JZOJ 4298. 【NOIP2015模拟11.2晚】我的天

    4298. [NOIP2015模拟11.2晚]我的天 (File IO): input:ohmygod.in output:ohmygod.out Time Limits: 1000 ms Memor ...

  4. 【NOIP2015模拟11.5】JZOJ8月5日提高组T2 Lucas的数列

    [NOIP2015模拟11.5]JZOJ8月5日提高组T2 Lucas的数列 题目 PS:\(n*n*T*T<=10^{18}\)而不是\(10^1*8\) 题解 题意: 给出\(n\)个元素的 ...

  5. 【NOIP2015模拟11.5】JZOJ8月5日提高组T1 俄罗斯套娃

    [NOIP2015模拟11.5]JZOJ8月5日提高组T1 俄罗斯套娃 题目 题解 题意就是说 将1~\(n\)排列,问有多少种方案使得序列的逆序对个数小于\(k\) 很容易想到DP 设\(f[i][ ...

  6. 【NOIP2015模拟11.2晚】JZOJ8月4日提高组T2 我的天

    [NOIP2015模拟11.2晚]JZOJ8月4日提高组T2 我的天 题目 很久很以前,有一个古老的村庄--xiba村,村子里生活着n+1个村民,但由于历届村长恐怖而且黑暗的魔法统治下,村民们各自过着 ...

  7. 【NOIP2015模拟11.4】JZOJ2020年8月6日提高组T2 最优交换

    [NOIP2015模拟11.4]JZOJ2020年8月6日提高组T2 最优交换 题目 题解 题意 有一个长度为\(n\)的正整数 最多可以进行\(k\)次操作 每次操作交换相邻两个位置上的数 问可以得 ...

  8. 【NOIP2015模拟11.4】JZOJ8月6日提高组T1 刷题计划

    [NOIP2015模拟11.4]JZOJ8月6日提高组T1 刷题计划 题目 题解 题意 有\(n\)道题,编号为1~\(n\) 给出\(m\)次操作 每次操作有3种类型 1 \(x\) 表示交了\(A ...

  9. 【NOIP2015模拟11.3】备用钥匙

    题目 你知道Just Odd Inventions社吗?这个公司的业务是"只不过是奇妙的发明(Just Odd Inventions)".这里简称为JOI社. JOI社有N名员工, ...

  10. JZOJ4307. 【NOIP2015模拟11.3晚】喝喝喝

    Description

随机推荐

  1. 解决linux vlc设置中文问题

    解决方法 sudo apt install vlc-l10n

  2. MySQL进阶实战5,为什么查询速度会慢

    一.先了解一下MySQL查询的执行过程 MySQL在查询时,它是由很多子任务组成的,每个子任务都会消耗一定的时间,如果要想优化查询,实际上要优化其子任务,可以消除一些子任务.减少子任务的执行次数.让子 ...

  3. 12V转5V降压芯片,12V转3.3V稳压芯片电路图

    12V转5V应用中,大多要求会输出电流高的,稳压LDO就不能满足了,需要使用DC-DC降压芯片来持续稳压5V,输出电流1000MA,2000MA,3000MA,5000MA等.不同的输出电流可以选择适 ...

  4. java中使用apache poi 读取 doc,docx,ppt,pptx,xls,xlsx,txt,csv格式的文件示例代码

    java使用apache poi 读取 doc,docx,ppt,pptx,xls,xlsx,txt,csv格式的文件示例代码 1.maven依赖添加 在 pom 文件中添加如下依赖 <depe ...

  5. 玩 ChatGPT 的正确姿势「GitHub 热点速览 v.22.49」

    火了一周的 ChatGPT,HG 不允许还有小伙伴不知道这个东西是什么?简单来说就是,你可以让它扮演任何事物,据说已经有人用它开始了颜色文学创作.因为它太火了,所以,本周特推在几十个带有"c ...

  6. 学习Django框架之前所需要了解的知识点

    目录 一: Web应用 1.Web应用程序什么? 2.软件开发架构 3.Web应用程序的优点 4.Web应用程序的缺点 5.B/S架构优点 6.Web框架本质 二:MVC和MTV模式 1.MVC设计模 ...

  7. python-docx操作word文档详解

    案例 官网地址: https://python-docx.readthedocs.io/en/latest/ pip install python-docx from docx import Docu ...

  8. 正则爬取豆瓣Top250数据存储到CSV文件(6行代码)

    利用正则爬取豆瓣TOP250电影信息 电影名字 电影年份 电影评分 评论人数 import requests import csv import re # 不算导包的话正式代码6行 存储到csv文件 ...

  9. [MySQL] 索引的使用、SQL语句优化策略

    目录 索引 什么是索引 索引的创建与删除 创建索引 删除索引 索引的使用 使用explain分析SQL语句 最佳左前缀 索引覆盖 避免对索引列进行额外运算 SQL语句优化 小表驱动大表 索引 什么是索 ...

  10. flutter报错The type of the function literal can't be inferred because the literal has a block as its body.A value of type 'String?' can't be assigned to a variable of type 'String'.

    flutter有一些报错如下 The type of the function literal can't be inferred because the literal has a block as ...