Advertisement
Combothermal

Untitled

Jul 28th, 2019
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. #include <ext/pb_ds/tree_policy.hpp>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7.  
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10.  
  11. typedef long long ll;
  12. typedef long double ld;
  13. typedef complex<ld> cd;
  14.  
  15. typedef pair<int, int> pi;
  16. typedef pair<ll,ll> pl;
  17. typedef pair<ld,ld> pd;
  18.  
  19. typedef vector<int> vi;
  20. typedef vector<ld> vd;
  21. typedef vector<ll> vl;
  22. typedef vector<pi> vpi;
  23. typedef vector<pl> vpl;
  24. typedef vector<cd> vcd;
  25.  
  26.  
  27. template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
  28.  
  29. #define FOR(i, a, b) for (int i=a; i<(b); i++)
  30. #define F0R(i, a) for (int i=0; i<(a); i++)
  31. #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
  32. #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
  33.  
  34. #define sz(x) (int)(x).size()
  35. #define mp make_pair
  36. #define pb push_back
  37. #define f first
  38. #define s second
  39. #define lb lower_bound
  40. #define ub upper_bound
  41. #define all(x) x.begin(), x.end()
  42. #define shandom_ruffle random_shuffle
  43.  
  44. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  45.  
  46. const int MOD = 1000000007;
  47. const ll INF = 1e18;
  48. const int MX = 100001; //check the limits, dummy
  49.  
  50. struct data {
  51. ll weight; int start, last, index;
  52. data(ll w, int S, int L, int I) {
  53. weight = w; start = S; last = L; index = I;
  54. }
  55. bool operator<(data other) const
  56. {
  57. return weight > other.weight;
  58. }
  59. };
  60.  
  61.  
  62. int main() {
  63. ios_base::sync_with_stdio(0); cin.tie(0);
  64.  
  65. int N, M, K; //cin >> N >> M >> K;
  66. N = 100000; M = 200000;
  67. K = 100000;
  68. vector<vpl> graph(N);
  69. F0R(i, N-1) {
  70. ll A, B, C;
  71. A = i; B = i+1;
  72. C = uniform_int_distribution<int>(1, 1000000000)(rng);
  73. graph[A].pb(mp(C, B));
  74. graph[B].pb(mp(C, A));
  75. }
  76. FOR(i, N-1, M) {
  77. ll A, B, C;
  78. A = uniform_int_distribution<int>(0, N-2)(rng);
  79. B = uniform_int_distribution<int>(A+1, N-1)(rng);
  80. C = uniform_int_distribution<int>(1, 1000000000)(rng);
  81. graph[A].pb(mp(C, B));
  82. graph[B].pb(mp(C, A));
  83. }
  84.  
  85. F0R(i, N) {
  86. sort(all(graph[i]));
  87. }
  88.  
  89. priority_queue<data> pq; //-weight, edge index, start, next-to-last.
  90. K *= 2;
  91. set<pl> visited;
  92. F0R(i, N) {
  93. pq.push(data(graph[i][0].f, i, i, 0));
  94. visited.insert(mp(i, i));
  95. }
  96.  
  97. while (!pq.empty()) {
  98. data cur = pq.top();
  99. pq.pop();
  100. int nxt = graph[cur.last][cur.index].s;
  101. if (!visited.count(mp(cur.start, graph[cur.last][cur.index].s))) {
  102. visited.insert(mp(cur.start, graph[cur.last][cur.index].s));
  103. //cout << cur.start << " " << nxt << " " << cur.weight << endl;
  104. K--;
  105. if (K == 0) {
  106. cout << cur.weight << endl; return 0;
  107. }
  108. pq.push(data(cur.weight + graph[nxt][0].f, cur.start, nxt, 0));
  109. }
  110. if (cur.index + 1 < sz(graph[cur.last])) {
  111. 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));
  112. }
  113.  
  114. }
  115.  
  116. return 0;
  117. }
  118.  
  119. // read the question correctly (ll vs int)
  120. // template by bqi343
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement