CF 1196F K-th Path

题目描述

https://codeforces.com/problemset/problem/1196/F

Solution

我们考虑答案一定小于等于第 $k$ 小的边的长度

所以我们直接将前 $k$ 小的边拿出来跑 $floyd$ 即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#define maxn 200010
#define maxm 200010
#define maxk 810
#define ll long long
#define INF 1000000000000000000ll
using namespace std;

int n, m, k;

int id[maxn], cnt;

struct Edge {
int fr, to, w;

friend bool operator < (const Edge &u, const Edge &v) { return u.w < v.w; }
} E[maxm];

ll g[maxk][maxk];
void init_g(int n) {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) g[i][j] = i == j ? 0 : INF;
}

void floyd(int n) {
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> m >> k; init_g(2 * k);
for (int i = 1; i <= m; ++i) cin >> E[i].fr >> E[i].to >> E[i].w;
sort(E + 1, E + m + 1);
for (int i = 1; i <= min(m, k); ++i) {
int u = E[i].fr, v = E[i].to, w = E[i].w;
if (!id[u]) id[u] = ++cnt;
if (!id[v]) id[v] = ++cnt;
u = id[u]; v = id[v];
g[u][v] = g[v][u] = min(g[u][v], 1ll * w);
}
floyd(cnt); priority_queue<ll> Q;
for (int i = 1; i <= cnt; ++i)
for (int j = i + 1; j <= cnt; ++j) {
ll t = g[i][j]; if (t == INF) continue;
if (Q.size() < k) Q.push(t);
else if (Q.top() > t) Q.pop(), Q.push(t);
}
cout << Q.top() << "\n";
return 0;
}