답안 #1059605

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1059605 2024-08-15T06:03:58 Z vjudge1 Cities (BOI16_cities) C++17
74 / 100
6000 ms 242652 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ii pair<int, int>
#define tp tuple<int, int, int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()

const int N = 2e5 + 5,
          INF = 2e16,
          mod = 1e9 + 7;
// code

int n, k, m, a[6], dp[N][33];
vector<ii> ad[N];
priority_queue<tp, vector<tp>, greater<>> pq;

int id(int i){
  for(int j = 0;  j< k; j++) if(a[j] == i) return j;
  return -1;
}

bool check(int msk1, int msk2){
  for(int i = 0; i < (1 << k); i++) if((msk1 >> i & 1) && (msk2 >> i & 1)) return false;
  return true;
}

int32_t main(){
  cin.tie(0)->sync_with_stdio(0);
//  #define task "test"
//  if(fopen(task ".inp", "r")){
//    freopen(task ".inp", "r", stdin);
//    freopen(task ".out", "w", stdout);
//  }

  cin >> n >> k >> m;

  for(int i = 0; i < k; i++) cin >> a[i];
  for(int i = 0; i < m; i++){
    int a, b, c; cin >> a >> b >> c;
    ad[a].push_back({b, c});
    ad[b].push_back({a, c});
  }

  sort(a, a + k);

  for(int i = 1; i <= n; i++)
    for(int j = 1; j < (1 << k); j++) dp[i][j] = INF;

  for(int i = 0; i < k; i++){
    dp[a[i]][1 << i] = 0;
    pq.push({0, 1 << i, a[i]});
  }

  while(pq.size()){
    auto [d, msk, u] = pq.top(); pq.pop();

    if(d != dp[u][msk]) continue;

//    cout << u << " " << d << " " << msk << endl;

    for(auto [v, c] : ad[u]){
      int bit = id(v);
      if(bit != -1 && !(msk >> bit & 1))
        if(d + c < dp[v][msk|(1 << bit)])
      pq.push({dp[v][msk|(1 << bit)] = d + c, msk|(1 << bit), v});

      for(int _msk = 0; _msk < (1 << k); _msk++)
        if(check(msk, _msk) && d + c + dp[v][_msk] < dp[v][msk|_msk])
          pq.push({dp[v][msk|_msk] = d + c + dp[v][_msk], msk|_msk, v});
    }
  }
  int res = INF;
  for(int i = 1; i <= n; i++){
    res = min(res, dp[i][(1 << k)-1]);
//    cerr << dp[i][(1 << k)-1] << endl;
  }
  cout << res << endl;

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 2 ms 5008 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 3 ms 5160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 793 ms 70304 KB Output is correct
2 Correct 680 ms 69484 KB Output is correct
3 Correct 294 ms 44552 KB Output is correct
4 Correct 128 ms 19156 KB Output is correct
5 Correct 285 ms 51648 KB Output is correct
6 Correct 52 ms 18524 KB Output is correct
7 Correct 5 ms 5720 KB Output is correct
8 Correct 3 ms 5468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 6356 KB Output is correct
2 Correct 13 ms 6424 KB Output is correct
3 Correct 9 ms 5916 KB Output is correct
4 Correct 11 ms 6232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2857 ms 143952 KB Output is correct
2 Correct 2649 ms 143296 KB Output is correct
3 Correct 1258 ms 62892 KB Output is correct
4 Correct 1779 ms 82056 KB Output is correct
5 Correct 721 ms 45128 KB Output is correct
6 Correct 516 ms 23032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6080 ms 242652 KB Time limit exceeded
2 Halted 0 ms 0 KB -