Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize ("O3")
- #pragma GCC target ("sse4")
- #include <bits/stdc++.h>
- #include <ext/pb_ds/tree_policy.hpp>
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- typedef long long ll;
- typedef long double ld;
- typedef complex<ld> cd;
- typedef pair<int, int> pi;
- typedef pair<ll,ll> pl;
- typedef pair<ld,ld> pd;
- typedef vector<int> vi;
- typedef vector<ld> vd;
- typedef vector<ll> vl;
- typedef vector<pi> vpi;
- typedef vector<pl> vpl;
- typedef vector<cd> vcd;
- template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
- #define FOR(i, a, b) for (int i=a; i<(b); i++)
- #define F0R(i, a) for (int i=0; i<(a); i++)
- #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
- #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
- #define sz(x) (int)(x).size()
- #define mp make_pair
- #define pb push_back
- #define f first
- #define s second
- #define lb lower_bound
- #define ub upper_bound
- #define all(x) x.begin(), x.end()
- #define shandom_ruffle random_shuffle
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- const int MOD = 1000000007;
- const ll INF = 1e18;
- const int MX = 100001; //check the limits, dummy
- struct data {
- ll weight; int start, last, index;
- data(ll w, int S, int L, int I) {
- weight = w; start = S; last = L; index = I;
- }
- bool operator<(data other) const
- {
- return weight > other.weight;
- }
- };
- int main() {
- ios_base::sync_with_stdio(0); cin.tie(0);
- int N, M, K; //cin >> N >> M >> K;
- N = 100000; M = 200000;
- K = 100000;
- vector<vpl> graph(N);
- F0R(i, N-1) {
- ll A, B, C;
- A = i; B = i+1;
- C = uniform_int_distribution<int>(1, 1000000000)(rng);
- graph[A].pb(mp(C, B));
- graph[B].pb(mp(C, A));
- }
- FOR(i, N-1, M) {
- ll A, B, C;
- A = uniform_int_distribution<int>(0, N-2)(rng);
- B = uniform_int_distribution<int>(A+1, N-1)(rng);
- C = uniform_int_distribution<int>(1, 1000000000)(rng);
- graph[A].pb(mp(C, B));
- graph[B].pb(mp(C, A));
- }
- F0R(i, N) {
- sort(all(graph[i]));
- }
- priority_queue<data> pq; //-weight, edge index, start, next-to-last.
- K *= 2;
- set<pl> visited;
- F0R(i, N) {
- pq.push(data(graph[i][0].f, i, i, 0));
- visited.insert(mp(i, i));
- }
- while (!pq.empty()) {
- data cur = pq.top();
- pq.pop();
- int nxt = graph[cur.last][cur.index].s;
- if (!visited.count(mp(cur.start, graph[cur.last][cur.index].s))) {
- visited.insert(mp(cur.start, graph[cur.last][cur.index].s));
- //cout << cur.start << " " << nxt << " " << cur.weight << endl;
- K--;
- if (K == 0) {
- cout << cur.weight << endl; return 0;
- }
- pq.push(data(cur.weight + graph[nxt][0].f, cur.start, nxt, 0));
- }
- if (cur.index + 1 < sz(graph[cur.last])) {
- pq.push(data(cur.weight - graph[cur.last][cur.index].f + graph[cur.last][cur.index + 1].f, cur.start, cur.last, cur.index + 1));
- }
- }
- return 0;
- }
- // read the question correctly (ll vs int)
- // template by bqi343
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement