답안 #554561

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
554561 2022-04-28T18:22:20 Z Soumya1 Cities (BOI16_cities) C++17
100 / 100
2681 ms 44880 KB
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 1e5 + 5;
const long long inf = 1e18;
using ll = long long;
int idx[mxN];
vector<pair<int, int>> ad[mxN];
vector<int> terminals;
ll d[mxN][32];
void testCase() {
  int n, k, m;
  cin >> n >> k >> m;
  for (int i = 0; i < k; i++) {
    int x;
    cin >> x;
    terminals.push_back(x);
    idx[x] = i;
  }
  while (m--) {
    int u, v, w;
    cin >> u >> v >> w;
    ad[u].push_back({v, w});
    ad[v].push_back({u, w});
  }
  for (int i = 1; i <= n; i++) {
    for (int mask = 0; mask < (1 << k); mask++) d[i][mask] = inf;
  }
  for (int t : terminals) d[t][1 << idx[t]] = 0;
  for (int mask = 1; mask < (1 << k); mask++) {
    for (int i = 1; i <= n; i++) {
      for (int s_mask = mask & (mask - 1); s_mask > 0; s_mask = mask & (s_mask - 1)) {
        d[i][mask] = min(d[i][mask], d[i][s_mask] + d[i][mask ^ s_mask]);
      }
    }
    priority_queue<pair<ll, int>> pq;
    for (int i = 1; i <= n; i++) pq.push({-d[i][mask], i});
    while (!pq.empty()) {
      auto [_, u] = pq.top();
      pq.pop();
      if (d[u][mask] != -_) continue;
      for (auto [v, w] : ad[u]) {
        if (d[v][mask] > d[u][mask] + w) {
          d[v][mask] = d[u][mask] + w;
          pq.push({-d[v][mask], v});
        }
      }
    }
  }
  ll ans = inf;
  for (int i = 1; i <= n; i++) ans = min(ans, d[i][(1 << k) - 1]);
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2680 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 713 ms 40712 KB Output is correct
2 Correct 658 ms 40820 KB Output is correct
3 Correct 419 ms 34696 KB Output is correct
4 Correct 60 ms 7736 KB Output is correct
5 Correct 325 ms 40508 KB Output is correct
6 Correct 55 ms 7684 KB Output is correct
7 Correct 5 ms 3028 KB Output is correct
8 Correct 3 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3028 KB Output is correct
2 Correct 7 ms 3028 KB Output is correct
3 Correct 5 ms 2948 KB Output is correct
4 Correct 5 ms 2956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1387 ms 40768 KB Output is correct
2 Correct 1367 ms 40720 KB Output is correct
3 Correct 898 ms 34800 KB Output is correct
4 Correct 739 ms 28820 KB Output is correct
5 Correct 170 ms 15396 KB Output is correct
6 Correct 62 ms 12700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2599 ms 40800 KB Output is correct
2 Correct 2681 ms 41040 KB Output is correct
3 Correct 2672 ms 44880 KB Output is correct
4 Correct 1792 ms 36816 KB Output is correct
5 Correct 1373 ms 28840 KB Output is correct
6 Correct 246 ms 15396 KB Output is correct
7 Correct 72 ms 12700 KB Output is correct