Submission #1059721

# Submission time Handle Problem Language Result Execution time Memory
1059721 2024-08-15T07:28:38 Z vjudge1 Cities (BOI16_cities) C++17
100 / 100
4059 ms 172852 KB
#include <bits/stdc++.h>

using namespace std;

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

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

// code

int n, k, m, a[6];
ll dp[N][33];
vector<ii> ad[N];
vector<int> nxt[33];
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});
  }
  for(int msk = 0; msk < (1 << k); msk++)
    for(int _msk = 0; _msk < (1 << k); _msk++)
      if(check(msk, _msk)) nxt[msk].push_back(_msk);

  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;
    if(id(u) != -1 && msk == (1 << k) - 1){
      cout << d;
      return 0;
    }

//    cerr << 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(auto _msk : nxt[msk])
        if(d + c + dp[v][_msk] < dp[v][msk|_msk])
          pq.push({dp[v][msk|_msk] = d + c + dp[v][_msk], msk|_msk, v});
    }
  }


  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 358 ms 57956 KB Output is correct
2 Correct 335 ms 57380 KB Output is correct
3 Correct 74 ms 37312 KB Output is correct
4 Correct 67 ms 14376 KB Output is correct
5 Correct 135 ms 45516 KB Output is correct
6 Correct 37 ms 13648 KB Output is correct
7 Correct 4 ms 5724 KB Output is correct
8 Correct 3 ms 5492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6104 KB Output is correct
2 Correct 7 ms 6068 KB Output is correct
3 Correct 5 ms 5688 KB Output is correct
4 Correct 5 ms 5844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1515 ms 107080 KB Output is correct
2 Correct 1215 ms 106720 KB Output is correct
3 Correct 581 ms 52644 KB Output is correct
4 Correct 761 ms 61080 KB Output is correct
5 Correct 151 ms 32568 KB Output is correct
6 Correct 89 ms 17120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4059 ms 172712 KB Output is correct
2 Correct 3958 ms 172852 KB Output is correct
3 Correct 3701 ms 172592 KB Output is correct
4 Correct 1380 ms 102000 KB Output is correct
5 Correct 2522 ms 159380 KB Output is correct
6 Correct 472 ms 49192 KB Output is correct
7 Correct 185 ms 23288 KB Output is correct