답안 #1064588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1064588 2024-08-18T15:03:59 Z duckindog Cities (BOI16_cities) C++17
100 / 100
4821 ms 73920 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 200'000 + 10,
          K = (1 << 5);
int n, m, k;
int mk[N], it;
vector<pair<int, int>> ad[N];
 
long long f[N][K];
 
int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);
 
  cin >> n >> k >> m;
  for (int i = 1; i <= k; ++i) { 
    int x; cin >> x;
    mk[x] = (1 << it++);
  }
 
  for (int i = 1; i <= m; ++i) { 
    int u, v, w; cin >> u >> v >> w;
    ad[u].push_back({v, w});
    ad[v].push_back({u, w});
  }
 
  { //dijsktra
    using T = tuple<long long, int, int>;
    priority_queue<T, vector<T>, greater<>> q;
    
    auto sktra = [&](int st) {
      for (int i = 1; i <= n; ++i) q.emplace(f[i][st], i, st);
      while (q.size()) { 
        auto [d, u, mask] = q.top(); q.pop();
        if (d != f[u][mask]) continue;
        for (const auto& [v, w] : ad[u]) { 
          int nMask = mask | mk[v];
          if (f[v][nMask] > d + w) q.emplace(f[v][nMask] = d + w, v, nMask);
        }
      }
    };
 
    memset(f, 14, sizeof f);
    for (int i = 1; i <= n; ++i) f[i][mk[i]] = 0;
 
    for (int mask = 0; mask < (1 << k); ++mask) { 
      for (int sub1 = mask; sub1; sub1 = (sub1 - 1) & mask) {
        int sub2 = mask ^ sub1;
        for (int i = 1; i <= n; ++i) 
          f[i][mask] = min(f[i][mask], f[i][sub1] + f[i][sub2]);
      }
      sktra(mask);
    }
  }
  
  long long answer = f[0][0];
  for (int i = 1; i <= n; ++i) answer = min(answer, f[i][(1 << k) - 1]);
 
  cout << answer << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 55120 KB Output is correct
2 Correct 22 ms 55136 KB Output is correct
3 Correct 21 ms 55132 KB Output is correct
4 Correct 25 ms 55128 KB Output is correct
5 Correct 24 ms 55128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 711 ms 69832 KB Output is correct
2 Correct 899 ms 69520 KB Output is correct
3 Correct 375 ms 62584 KB Output is correct
4 Correct 70 ms 63708 KB Output is correct
5 Correct 362 ms 69784 KB Output is correct
6 Correct 62 ms 63436 KB Output is correct
7 Correct 24 ms 55236 KB Output is correct
8 Correct 27 ms 55376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 55388 KB Output is correct
2 Correct 29 ms 55356 KB Output is correct
3 Correct 24 ms 55132 KB Output is correct
4 Correct 25 ms 55320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2075 ms 69796 KB Output is correct
2 Correct 2115 ms 73648 KB Output is correct
3 Correct 1155 ms 62664 KB Output is correct
4 Correct 944 ms 69312 KB Output is correct
5 Correct 258 ms 67788 KB Output is correct
6 Correct 87 ms 65484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4453 ms 73916 KB Output is correct
2 Correct 4821 ms 73920 KB Output is correct
3 Correct 4811 ms 73520 KB Output is correct
4 Correct 2546 ms 62660 KB Output is correct
5 Correct 2270 ms 69312 KB Output is correct
6 Correct 687 ms 67940 KB Output is correct
7 Correct 149 ms 65992 KB Output is correct