F. K-th Path ( Codeforces Round #575)

 

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 (1i<jn1≤i<j≤n), then you need to print the kk-th element in the sorted array consisting of all di,jdi,j, where 1i<jn1≤i<j≤n.

Input

The first line of the input contains three integers n,mn,m and kk (2n21052≤n≤2⋅105, n1mmin(n(n1)2,2105)n−1≤m≤min(n(n−1)2,2⋅105), 1kmin(n(n1)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 (1x,yn1≤x,y≤n, 1w1091≤w≤109, xyx≠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).

Output

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).

Examples
input
Copy
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
output
Copy
3
input
Copy
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
output
Copy
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;
}

猜你喜欢

转载自www.cnblogs.com/Shallow-dream/p/11427417.html