Submission #383062

#TimeUsernameProblemLanguageResultExecution timeMemory
383062HegdahlRelay Marathon (NOI20_relaymarathon)C++17
100 / 100
5649 ms266444 KiB
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#ifdef ENABLE_DEBUG
#include <debug.h>
#else
#define DEBUG(...) do {} while (0)
#endif

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ull = unsigned long long;
using lll = __int128;
using ulll = unsigned __int128;

using ld = long double;

template<typename T, size_t N> using ar = array<T, N>;

template<typename T, typename Cmp = less<T>>
using iset = tree<T, null_type, Cmp, rb_tree_tag,
		  tree_order_statistics_node_update, allocator<T>>;

#define REPSI(name, start, stop, step) for (ll name = start; name < (ll)stop; name += step)
#define REPS(name, start, stop) REPSI(name, start, stop, 1)
#define REP(name, stop) REPS(name, 0, stop)

#define RREPSI(name, start, stop, step) for (ll name = stop-1; name >= (ll)start; name -= step)
#define RREPS(name, start, stop) RREPSI(name, start, stop, 1)
#define RREP(name, stop) RREPS(name, 0, stop)

template<typename T> void cins(T &first) { cin >> first; }
template<typename T, typename... Ts> void cins(T &first, T &second, Ts&... rest) {
  cin >> first;
  cins(second, rest...);
}

#define GET(type, ...) type __VA_ARGS__; cins(__VA_ARGS__)
#define GETI(...) GET(int, __VA_ARGS__)
#define GETLL(...) GET(ll, __VA_ARGS__)
#define GETS(...) GET(string, __VA_ARGS__)
#define GETD(...) GET(double, __VA_ARGS__)
#define GETC(...) GET(char, __VA_ARGS__)

struct hsh {
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    x += FIXED_RANDOM;
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
};

const ll INF = 1LL<<60;
const int mxN = 1e5;

vector<ar<ll, 2>> g[mxN];

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class IsStart, class IsTarget>
ar<ll, 3> solve(ll n, IsStart is_start, IsTarget is_target, ll cut) {
  
  vector<bool> vis(n);
  priority_queue<ar<ll, 3>, vector<ar<ll, 3>>, greater<ar<ll, 3>>> pq;
  REP(i, n) if (is_start(i)) pq.push({0, i, i});

  while (pq.size()) {
    auto [dhere, cur, source] = pq.top();
    pq.pop();

    if (dhere > cut) return {INF, -1, -1};

    if (vis[cur]) continue;
    if (is_target(cur)) return {dhere, cur, source};

    vis[cur] = true;

    for (auto [nxt, w] : g[cur]) {
      if (vis[nxt]) continue;
      pq.push({dhere+w, nxt, source});
    }
  }

  return {INF, -1, -1};
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);

  auto t0 = chrono::steady_clock::now();

  GETI(n, m, k);
  REP(mm, m) {
    GETI(i, j, w); --i, --j;
    g[i].push_back({j, w});
    g[j].push_back({i, w});
  }

  vector<ll> a(k), pof(n, -1);
  REP(i, k) cin >> a[i], --a[i];

  ll ans = INF;

  while (true) {
    auto t1 = chrono::steady_clock::now();
    if (chrono::duration_cast<chrono::milliseconds>(t1-t0).count() > 5500) break;

    shuffle(a.begin(), a.end(), rng);
    REP(i, k) pof[a[i]] = i;

    auto [d1, target1, source1] = solve(n,
        [&](ll i){return pof[i] >= 0*k/4 && pof[i] < 1*k/4;},
        [&](ll i){return pof[i] >= 2*k/4 && pof[i] < 3*k/4;},
        ans
    );                                                  

    if (target1 == -1) continue;

    auto [d2, target2, source2] = solve(n,                                    
        [&](ll i){return pof[i] >= 0*k/4 && pof[i] < 2*k/4 && i != target1 && i != source1;},
        [&](ll i){return pof[i] >= 2*k/4 && pof[i] < 4*k/4 && i != target1 && i != source1;},
        ans-d1
    );

    ans = min(ans, d1+d2);
  }

  cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...