F. K-th Path
题意:
将
n
n
n 个点的全源最短路排序,求第
k
k
k 小最短路
思路:
第
k
k
k 最小最短路一定出现在前
k
k
k 小边形成的最短路中
先在当前存在的所有边中找到前
k
k
k 小的边进行保存,因为数值大小可以达到
2
×
1
0
5
2\times 10^5
2×105,跑
f
l
o
y
d
floyd
floyd 开二维数组编号放不下,所以点的编号进行离散化处理,然后对这
k
k
k 条边进行保存在图中,然后跑
f
l
o
y
d
floyd
floyd,存起来全源最短路,排序取第
k
k
k 小即可
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
ll dis[809][809];
struct node{
int x, y, w;
bool operator<(const node &B)const{
return w < B.w;
}
}e[maxn];
void work()
{
int k;cin >> n >> m >> k;
for(int i = 1; i <= m; ++i){
cin >> e[i].x >> e[i].y >> e[i].w;
}
sort(e + 1, e + 1 + m);
int cnt = 1;
vector <int> v;
for(int i = 1; i <= min(m, 1ll * k); ++i){
v.push_back(e[i].x);v.push_back(e[i].y);
}
sort(all(v));
v.erase(unique(all(v)), v.end());
map <int,int> ma;
for(int i = 0; i < v.size(); ++i) ma[v[i]] = i + 1;
mem(dis, 0x3f);
for(int i = 1; i <= min(m, 1ll * k); ++i){
int x = ma[e[i].x], y = ma[e[i].y];
dis[x][y] = dis[y][x] = e[i].w;
}
m = v.size();
for(int k = 1; k <= m; ++k)
for(int i = 1; i <= m; ++i)
for(int j = 1; j <= m; ++j)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
vector <ll> ans;
for(int i = 1; i <= m; ++i)
for(int j = i + 1; j <= m; ++j)
if(dis[i][j] != INF) ans.push_back(dis[i][j]);
sort(all(ans));
cout << ans[k - 1];
}
int main()
{
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}