답안 #1058915

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1058915 2024-08-14T15:03:05 Z duckindog Cities (BOI16_cities) C++17
0 / 100
598 ms 36200 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 100'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); it += 1;
  }

  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 = 0; sub1 < (1 << k); ++sub1) {
        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 3 ms 27740 KB Output is correct
2 Correct 3 ms 27740 KB Output is correct
3 Incorrect 3 ms 27740 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 123 ms 36044 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 27740 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 257 ms 36200 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 598 ms 36108 KB Output isn't correct
2 Halted 0 ms 0 KB -