Submission #653419

# Submission time Handle Problem Language Result Execution time Memory
653419 2022-10-26T20:17:26 Z tvladm2009 Cities (BOI16_cities) C++14
100 / 100
2512 ms 51040 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
const int N = (int) 2e5 + 7;
const int K = 5;
const int INF = (ll) 1e18;
int dp[1 << K][N], special[N];
vector<pair<int, int>> g[N];
bool vis[N];
int n, k, m;

signed main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  cin >> n >> k >> m;
  vector<int> cities(k + 1);
  for (int i = 1; i <= k; i++) {
    cin >> cities[i];
    special[cities[i]] = i;
  }
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    g[x].push_back({y, z});
    g[y].push_back({x, z});
  }
  for (int mask = 0; mask < (1 << k); mask++) {
    for (int i = 0; i <= n; i++) {
      dp[mask][i] = INF;
    }
  }
  for (int i = 1; i <= k; i++) {
    dp[(1 << (i - 1))][cities[i]] = 0;
  }
  for (int i = 1; i <= n; i++) {
    dp[0][i] = 0;
  }

  for (int mask = 0; mask < (1 << k); mask++) {
    for (int i = 1; i <= n; i++) {
      for (int submask = mask; submask > 0; submask = (submask - 1) & mask) {
        ll newmask = (mask ^ submask);
        if (special[i]) {
          newmask ^= (1 << (special[i] - 1));
        }
        dp[mask][i] = min(dp[mask][i], dp[newmask][i] + dp[submask][i]);
      }
    }

    priority_queue<pair<int, int>> pq;
    for (int i = 0; i <= n; i++) {
      vis[i] = 0;
      pq.push({-dp[mask][i], i});
    }
    while (!pq.empty()) {
      pair<int, int> u = pq.top();
      pq.pop();
      if (vis[u.second]) {
        continue;
      }
      vis[u.second] = 1;
      for (auto &v : g[u.second]) {
        if (dp[mask][u.second] + v.second < dp[mask][v.first]) {
          dp[mask][v.first] = dp[mask][u.second] + v.second;
          pq.push({-dp[mask][v.first], v.first});
        }
      }
    }
  }
  ll ret = INF;
  for (int i = 0; i <= n; i++) {
    ret = min(ret, dp[(1 << k) - 1][i]);
  }
  cout << ret;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5024 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 656 ms 28312 KB Output is correct
2 Correct 588 ms 27860 KB Output is correct
3 Correct 391 ms 20332 KB Output is correct
4 Correct 63 ms 14228 KB Output is correct
5 Correct 325 ms 25100 KB Output is correct
6 Correct 56 ms 14132 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 5 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5332 KB Output is correct
2 Correct 10 ms 5436 KB Output is correct
3 Correct 6 ms 5332 KB Output is correct
4 Correct 8 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1168 ms 34648 KB Output is correct
2 Correct 1114 ms 34212 KB Output is correct
3 Correct 786 ms 26640 KB Output is correct
4 Correct 580 ms 29844 KB Output is correct
5 Correct 176 ms 20696 KB Output is correct
6 Correct 70 ms 19948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2180 ms 47284 KB Output is correct
2 Correct 2050 ms 47292 KB Output is correct
3 Correct 2512 ms 51040 KB Output is correct
4 Correct 1783 ms 41356 KB Output is correct
5 Correct 1198 ms 36240 KB Output is correct
6 Correct 266 ms 22216 KB Output is correct
7 Correct 93 ms 20412 KB Output is correct