传送门

题意:

这道题把我看得懵懵的(不敢相信),其实就是给你n个点和m条边(无向图),你要找出来任意两点之间的的最短距离,然后再从其中找出来第k个最小值

题解:

正常思维就是floyd多源最短路算法跑一遍,然后把任意两点之间的距离取出来放在数组里面,再排序。之后打印出第k个就可以了

但是n的范围是2e5,然而数组开不了这么大的,所以这里有一点优化的。因为要求第k大的距离,所以我们对所有边排序,取出来前k条边,用这k条边的端点(可以离散化把点的值变小)来重新构造一个图

至于为什么可以这样做,我们可以实践一下,如果这k条边互相不相连,那么第k个大的边(而且这个边还是最短的,正好相当于两点之间的最短路),正好符合题意(因为我们这k条边是在所有边中最小的k个,其他的边肯定要大于等于它)

如果相连了,那么也可以构造出来新的更短的距离,只有这个新的两点之间的距离小于等于咱们挑出来的边才会使用到,所以这样也符合题意

代码:

  1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<map>
6 #include<set>
7 #include<vector>
8 #include<algorithm>
9 #include<queue>
10 #include<unordered_map>
11 #include<list>
12 using namespace std;
13 #define ll long long
14 const int mod=1e9+7;
15 const ll int inf=1e18+7;
16
17 const int maxn=4e5+5;
18
19 typedef struct
20 {
21 int u;
22 int v;
23 ll int w;
24 }St;
25 St sum[maxn];
26
27 bool cmp(const St &a,const St &b)
28 {
29 return a.w < b.w;
30 }
31
32 int n,m,k;
33
34 ll int e[2005][2005];
35
36 void init()
37 {
38 for(int i=0;i<=1505;i++)
39 {
40 for(int j=0;j<=1505;j++)
41 {
42 if(i==j)
43 e[i][j]=0;
44 else
45 e[i][j]=inf;
46 }
47 }
48 }
49
50 int main()
51 {
52 ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
53
54 init();
55
56 cin>>n>>m>>k;
57
58 vector<ll int>v;
59
60 for(int i=0;i<m;i++)
61 cin>>sum[i].u>>sum[i].v>>sum[i].w;
62
63 sort(sum,sum+m,cmp);
64
65 for(int i=0;i<min(m,k);i++)
66 {
67 v.push_back(sum[i].u);
68 v.push_back(sum[i].v);
69 }
70
71 //离散化
72 sort(v.begin(),v.end());//排序
73 v.erase(unique(v.begin(),v.end()),v.end());//去重
74
75 int now=v.size();
76
77 for(int i=0;i<min(m,k);i++)
78 {
79 sum[i].u=lower_bound(v.begin(),v.end(),sum[i].u)-v.begin()+1;//加一防止点从0开始
80 sum[i].v=lower_bound(v.begin(),v.end(),sum[i].v)-v.begin()+1;
81
82 e[sum[i].u][sum[i].v]=sum[i].w;
83 e[sum[i].v][sum[i].u]=sum[i].w;
84
85 }
86
87 for(int k=1;k<=now;k++)
88 {
89 for(int i=1;i<=now;i++)
90 {
91 for(int j=1;j<=now;j++)
92 {
93 if(e[i][k]+e[k][j]<e[i][j])
94 {
95 e[i][j]=e[i][k]+e[k][j];
96 }
97 }
98 }
99 }
100
101 vector<ll int>edge;
102
103 for(int i=1;i<=now;i++)
104 {
105 for(int j=i+1;j<=now;j++)
106 {
107 edge.push_back(e[i][j]);
108 }
109 }
110
111 sort(edge.begin(),edge.end());;
112
113 cout<<edge[k-1]<<endl;
114
115 return 0;
116 }

Codeforces Round #575 (Div. 3) F. K-th Path的更多相关文章

  1. Codeforces Round #575 (Div. 3) 昨天的div3 补题

    Codeforces Round #575 (Div. 3) 这个div3打的太差了,心态都崩了. B. Odd Sum Segments B 题我就想了很久,这个题目我是找的奇数的个数,因为奇数想分 ...

  2. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  3. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  4. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  5. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  6. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  7. Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)

    题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...

  8. Codeforces Round #552 (Div. 3) F题

    题目网址:http://codeforces.com/contest/1154/problem/F 题目大意:给出n,m,k,n是物体的个数,m是优惠方式的种数,k是需要购买的物体个数, 然后给出n个 ...

  9. Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂

    https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...

随机推荐

  1. springBoot实现redis分布式锁

    参考:https://blog.csdn.net/weixin_44634197/article/details/108308395 .. 使用redis的set命令带NX(not exist)参数实 ...

  2. 【ORA】ORA-4031错误分析和解决办法

    1. ORA-4031错误的原因,一般是大量的hard parse导致了shared pool中的free list中产生大量的内存小碎片,当一个需要很大内存来进行hard parse的sql语句到来 ...

  3. 【葵花宝典】All-in-One模式安装KubeSphere

    1.准备 Linux 机器 2.google api受限下载 KubeKey export KKZONE=cn curl -sfL https://get-kk.kubesphere.io | VER ...

  4. oracle dataguard搭建

    搭建前环境准备 1.查看主库的oracle的uid和gid并在备库创建用户 # 主库查看oracle $ id oracle uid=54321(oracle) gid=54321(oinstall) ...

  5. CMU数据库(15-445)Lab0-环境搭建

    0.写在前面 从这篇文章开始.开一个新坑,记录以下自己做cmu数据库实验的过程,同时会分析一下除了要求我们实现的代码之外的实验自带的一些代码.争取能够对实现一个数据库比较了解.也希望能写进简历.让自己 ...

  6. EFCore 5 新特性 —— Savepoints

    EFCore 5 中的 Savepoints Intro EFCore 5中引入了一个新特性,叫做 Savepoints,主要是事务中使用,个人感觉有点类似于 Windows 上的系统还原点,如果事务 ...

  7. 记录list.remove()和list.pop()

    list.remove(obj):这个是移除列表中某个值的第一个匹配项 list.pop(index):这个是移除列表中下标为index的元素 当元素全是数字或者有数字时注意区分.

  8. 前端面试之CSS常用的选择器!

    前端面试之CSS常用的选择器! 标签选择器 <style> /* <!-- 标签选择器 :写上标签名 -->*/ p { color: green; } div { color ...

  9. CSSmargin击穿问题(子元素margin-top会影响父元素)

    最近写一个H5页面的时候发现了这个被忽视的问题,一时没想到什么原因,搜了半天,记录一下,方便他人踩坑.唉,有些东西不用就忘. 一.问题描述 <div class="container& ...

  10. spring restTemplate 进行http请求的工具类封装

    本文为博主原创,未经允许不得转载: 1.对常用调用的方法进行封装: import org.springframework.http.HttpHeaders; import com.alibaba.fa ...