답안 #969392

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
969392 2024-04-25T05:40:16 Z Spade1 Cities (BOI16_cities) C++14
100 / 100
2573 ms 42628 KB
#pragma once
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pld> vpld;
typedef vector<vi> vvi;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

template<typename T> using pq = priority_queue<T>;
template<typename T> using pqg = priority_queue<T, vector<T>, greater<T>>;

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
#define ins insert

template<typename T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

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

const int MOD = 1e9 + 7;
const int INF = 0x3fffffff;
const ll LINF = 0x1fffffffffffffff;
const char nl = '\n';
const int MX = 1e5 + 3;

vpii adj[MX];
ll dp[MX][32], imp[MX];

void solve() {
  int n, k, m; cin >> n >> k >> m;
  for (int i = 0; i < k; ++i) cin >> imp[i];
  for (int i = 1; i <= m; ++i) {
    int u, v, w; cin >> u >> v >> w;
    adj[u].pb({v, w}); adj[v].pb({u, w});
  }
  
  for (int i = 1; i <= n; ++i) for (int mask = 0; mask < 32; ++mask) dp[i][mask] = LINF;
  //base cases
  for (int i = 0; i < k; ++i) dp[imp[i]][(1<<i)] = 0;
  for (int bitmask = 0; bitmask < (1<<k); ++bitmask) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < k; ++j) {
        if (bitmask & (1<<j)) {
          ckmin(dp[i][bitmask], dp[i][bitmask^(1<<j)] + dp[i][(1<<j)]);
        }
      }
    }

    pqg<pll> pq;
    for (int i = 1; i <= n; ++i) pq.push({dp[i][bitmask], i});
    while (!pq.empty()) {
      auto [W, a] = pq.top(); pq.pop();
      if (dp[a][bitmask] < W) continue;
      for (auto [b, w] : adj[a]) {
        if (ckmin(dp[b][bitmask], dp[a][bitmask] + w)) pq.push({dp[b][bitmask], b});
      }
    }
  }

  ll ans = LINF;
  for (int i = 1; i <= n; ++i) ckmin(ans, dp[i][(1<<k)-1]);
  cout << ans << nl;
}

int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(0); cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) { solve(); }
  return 0;
}

Compilation message

cities.cpp:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
cities.cpp: In function 'void solve()':
cities.cpp:71:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   71 |       auto [W, a] = pq.top(); pq.pop();
      |            ^
cities.cpp:73:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   73 |       for (auto [b, w] : adj[a]) {
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5724 KB Output is correct
2 Correct 1 ms 5720 KB Output is correct
3 Correct 1 ms 5724 KB Output is correct
4 Correct 2 ms 5724 KB Output is correct
5 Correct 1 ms 5724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 653 ms 42628 KB Output is correct
2 Correct 674 ms 42268 KB Output is correct
3 Correct 441 ms 35412 KB Output is correct
4 Correct 61 ms 10580 KB Output is correct
5 Correct 316 ms 42148 KB Output is correct
6 Correct 43 ms 10484 KB Output is correct
7 Correct 4 ms 5980 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5976 KB Output is correct
2 Correct 5 ms 5980 KB Output is correct
3 Correct 4 ms 5796 KB Output is correct
4 Correct 4 ms 5976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1319 ms 42260 KB Output is correct
2 Correct 1333 ms 41116 KB Output is correct
3 Correct 820 ms 35200 KB Output is correct
4 Correct 632 ms 27784 KB Output is correct
5 Correct 122 ms 14112 KB Output is correct
6 Correct 49 ms 12248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2573 ms 42276 KB Output is correct
2 Correct 2520 ms 42268 KB Output is correct
3 Correct 2464 ms 42400 KB Output is correct
4 Correct 1653 ms 35388 KB Output is correct
5 Correct 1192 ms 27604 KB Output is correct
6 Correct 187 ms 14116 KB Output is correct
7 Correct 76 ms 12280 KB Output is correct