Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define pb push_back
- #define ll long long
- #define ld long double
- #define F first
- #define S second
- const ll inf = 2e12;
- int main() {
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- freopen("phoneline.in", "r", stdin);
- freopen("phoneline.out", "w", stdout);
- #endif
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- ll n, m, k;
- cin >> n >> m >> k;
- vector <pair <ll ,ll > > g[n];
- vector <ll> rib;
- for (ll i = 0; i < m; i++){
- ll x, y, z;
- cin >> x >> y >> z;
- x --;
- y --;
- g[x].pb({y, z});
- g[y].pb({x, z});
- rib.pb(z);
- }
- rib.pb(0);
- sort (rib.begin() , rib.end());
- ll l = 0, r = m;
- while (l + 1 < r){
- ll mid = (l + r) / 2;
- priority_queue <pair <ll ,ll> > q;
- q.push({0 , 0});
- vector <ll> d(n, inf);
- d[0] = 0;
- while (!q.empty()){
- ll rast = -q.top().F , v = q.top().S;
- q.pop();
- if (rast > d[v])continue;
- for (auto to : g[v]){
- if (d[v] + (to.S > mid) < d[to.F]){
- d[to.F] = d[v] + (to.S > mid);
- q.push({-d[to.F] , to.F});
- }
- }
- }
- if (d[n - 1] > k){
- l = mid + 1;
- }
- else{
- r = mid;
- }
- }
- for (ll i = l; i <= r; i++){
- priority_queue <pair <ll ,ll> > q;
- q.push({0 , 0});
- vector <ll> d(n, inf);
- d[0] = 0;
- while (!q.empty()){
- ll rast = -q.top().F , v = q.top().S;
- q.pop();
- if (rast > d[v])continue;
- for (auto to : g[v]){
- if (d[v] + (to.S > i) < d[to.F]){
- d[to.F] = d[v] + (to.S > i);
- q.push({-d[to.F] , to.F});
- }
- }
- }
- if (d[n - 1] <= k){
- cout << i << endl;
- return 0;
- }
- }
- cout << "-1" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement