You are given a connected undirected weighted graph consisting of nn vertices and mm edges.
You need to print the kk-th smallest shortest path in this graph (paths from the vertex to itself are not counted, paths from ii to jj and from jjto ii are counted as one).
More formally, if dd is the matrix of shortest paths, where di,jdi,j is the length of the shortest path between vertices ii and jj (1≤i<j≤n1≤i<j≤n), then you need to print the kk-th element in the sorted array consisting of all di,jdi,j, where 1≤i<j≤n1≤i<j≤n.
The first line of the input contains three integers n,mn,m and kk (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤min(n(n−1)2,2⋅105)n−1≤m≤min(n(n−1)2,2⋅105), 1≤k≤min(n(n−1)2,400)1≤k≤min(n(n−1)2,400) — the number of vertices in the graph, the number of edges in the graph and the value of kk, correspondingly.
Then mm lines follow, each containing three integers xx, yy and ww (1≤x,y≤n1≤x,y≤n, 1≤w≤1091≤w≤109, x≠yx≠y) denoting an edge between vertices xx and yy of weight ww.
It is guaranteed that the given graph is connected (there is a path between any pair of vertices), there are no self-loops (edges connecting the vertex with itself) and multiple edges (for each pair of vertices xx and yy, there is at most one edge between this pair of vertices in the graph).
Print one integer — the length of the kk-th smallest shortest path in the given graph (paths from the vertex to itself are not counted, paths from ii to jj and from jj to ii are counted as one).
6 10 5 2 5 1 5 3 9 6 2 2 1 3 1 5 1 8 6 5 10 1 6 5 6 4 6 3 6 2 3 4 5
3
7 15 18 2 6 3 5 7 4 6 5 4 3 6 9 6 7 7 1 6 4 7 1 6 7 2 1 4 3 2 3 2 8 5 3 6 2 5 5 3 7 9 4 1 8 2 1 1
9
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define INF 0x3f3f3f3f #define N 500005 struct Node { ll index; ll u,v,w; } arc; bool cmp(Node x,Node y) { return x.w<y.w; } vector<Node>e; vector<ll>vert; ll dist[1010][1010]; int main() { ll n,m,k; scanf("%lld%lld%lld",&n,&m,&k); for(int i=1; i<=m; i++) { ll a,b,c; scanf("%lld%lld%lld",&a,&b,&c); // a--;b--; arc.u=a; arc.v=b; arc.w=c; e.push_back(arc); } sort(e.begin(),e.end(),cmp); for(int i=0; i<min(m,k); i++) { vert.push_back(e[i].u); vert.push_back(e[i].v); } sort(vert.begin(),vert.end()); vert.resize(unique(vert.begin(), vert.end()) - vert.begin()); int cntv = vert.size(); memset(dist,0x3f,sizeof(dist)); for(int i=0; i<=cntv; i++) { dist[i][i]=0; } for (int i = 0; i < min(m, k); ++i) { int x = lower_bound(vert.begin(), vert.end(), e[i].u) - vert.begin(); int y = lower_bound(vert.begin(), vert.end(), e[i].v) - vert.begin(); // cout<<x<<" "<<y<<" "<<e[i].w<<endl; dist[x][y] = dist[y][x] = min(dist[x][y], e[i].w); } for (int z = 0; z <cntv; ++z) { for (int x = 0; x <cntv; ++x) { for (int y = 0; y <cntv; ++y) { dist[x][y] = min(dist[x][y], dist[x][z] + dist[z][y]); } } } vector<ll> res; for (int i = 0; i <= cntv; ++i) { for (int j = 0; j < i; ++j) { res.push_back(dist[i][j]); } } sort(res.begin(), res.end()); cout << res[k - 1] << endl; return 0; }