답안 #734856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
734856 2023-05-03T07:20:42 Z dong_liu Cities (BOI16_cities) C++17
100 / 100
3885 ms 107960 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5, K = 5;

int main() {
  ios::sync_with_stdio(false); cin.tie(NULL);
  int n, k, m; cin >> n >> k >> m;
  static int v[K];
  for (int i = 0; i < k; i++) cin >> v[i], v[i]--;
  static vector<pair<int, int>> g[N];
  while (m--) {
    int i, j, w; cin >> i >> j >> w, i--, j--;
    g[i].push_back({j, w}), g[j].push_back({i, w});
  }
  static long long d[K][N];
  for (int s = 0; s < k; s++) {
    long long *t = d[s];
    priority_queue<pair<long long, int>> q;
    memset(t, 0x3f, n * 8);
    q.push({-(t[v[s]] = 0), v[s]});
    while (!q.empty()) {
      auto [x, i] = q.top(); x *= -1, q.pop();
      if (t[i] != x) continue;
      for (auto [j, w] : g[i])
        if (x + w < t[j])
          q.push({-(t[j] = x + w), j});
    }
  }
  static long long f[N][1 << K];
  for (int i = 0; i < n; i++) memset(f[i], 0x3f, (1 << k) * 8);
  priority_queue<tuple<long long, int, int>> q;
  for (int i = 0; i < k; i++) q.push({-(f[v[i]][1 << i] = 0), v[i], 1 << i});
  while (!q.empty()) {
    auto [x, i, b] = q.top(); x *= -1; q.pop();
    if (f[i][b] != x) continue;
    for (auto [j, w] : g[i])
      if (x + w < f[j][b])
        q.push({-(f[j][b] = x + w), j, b});
    for (int s = 0; s < k; s++)
      if ((b >> s & 1) == 0 && x + d[s][i] < f[i][b | (1 << s)])
        q.push({-(f[i][b | 1 << s] = x + d[s][i]), i, b | (1 << s)});
  }
  long long ans = LLONG_MAX;
  for (int i = 0; i < n; i++) ans = min(ans, f[i][(1 << k) - 1]);
  cout << ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 836 ms 44540 KB Output is correct
2 Correct 795 ms 48604 KB Output is correct
3 Correct 447 ms 39548 KB Output is correct
4 Correct 76 ms 11636 KB Output is correct
5 Correct 374 ms 43972 KB Output is correct
6 Correct 64 ms 11160 KB Output is correct
7 Correct 5 ms 3284 KB Output is correct
8 Correct 4 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 3412 KB Output is correct
2 Correct 9 ms 3412 KB Output is correct
3 Correct 6 ms 3412 KB Output is correct
4 Correct 6 ms 3160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1846 ms 70208 KB Output is correct
2 Correct 1633 ms 73756 KB Output is correct
3 Correct 1189 ms 52648 KB Output is correct
4 Correct 948 ms 43288 KB Output is correct
5 Correct 235 ms 22296 KB Output is correct
6 Correct 73 ms 13184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3856 ms 103648 KB Output is correct
2 Correct 3885 ms 107960 KB Output is correct
3 Correct 3474 ms 107556 KB Output is correct
4 Correct 2688 ms 102788 KB Output is correct
5 Correct 2070 ms 60052 KB Output is correct
6 Correct 435 ms 22540 KB Output is correct
7 Correct 90 ms 13636 KB Output is correct