Submission #554320

# Submission time Handle Problem Language Result Execution time Memory
554320 2022-04-28T06:59:13 Z Soumya1 Cities (BOI16_cities) C++17
74 / 100
6000 ms 91424 KB
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 1e5 + 5;
const long long inf = 1e18;
using ll = long long;
int idx[mxN];
vector<pair<int, int>> ad[mxN];
vector<int> terminals;
ll dist[5][mxN];
ll d[mxN][32];
int n, k;
void dijkstra(int s) {
  int t = idx[s];
  for (int i = 1; i <= n; i++) dist[t][i] = inf;
  priority_queue<pair<ll, int>> pq;
  pq.push({0, s});
  dist[t][s] = 0;
  while (!pq.empty()) {
    auto [dd, u] = pq.top();
    pq.pop();
    if (dist[t][u] != -dd) continue;
    for (auto [v, w] : ad[u]) {
      if (dist[t][v] > dist[t][u] + w) {
        dist[t][v] = dist[t][u] + w;
        pq.push({-dist[t][v], v});
      }
    }
  }
}
ll solve(int s) {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << k); j++) d[i][j] = inf;
  }
  priority_queue<tuple<ll, int, int>> pq;
  pq.push({0, s, 1 << idx[s]});
  d[s][1 << idx[s]] = 0;
  while (!pq.empty()) {
    auto [_, u, mask] = pq.top();
    pq.pop();
    if (d[u][mask] != -_) continue;
    for (int j = 0; j < k; j++) {
      if (mask >> j & 1) continue;
      int new_mask = mask | (1 << j);
      if (d[u][new_mask] > d[u][mask] + dist[j][u]) {
        d[u][new_mask] = d[u][mask] + dist[j][u];
        pq.push({-d[u][new_mask], u, new_mask});
      }
    }
    for (auto [v, w] : ad[u]) {
      if (d[v][mask] > d[u][mask] + w) {
        d[v][mask] = d[u][mask] + w;
        pq.push({-d[v][mask], v, mask});
      }
    }
  }
  ll ans = inf;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, d[i][(1 << k) - 1]);
  }
  return ans;
}
void testCase() {
  int m;
  cin >> n >> k >> m;
  for (int i = 0; i < k; i++) {
    int x;
    cin >> x;
    terminals.push_back(x);
    idx[x] = i;
  }
  while (m--) {
    int u, v, w;
    cin >> u >> v >> w;
    ad[u].push_back({v, w});
    ad[v].push_back({u, w});
  }
  for (int t : terminals) {
    dijkstra(t);
  }
  ll ans = inf;
  for (int t : terminals) {
    ans = min(ans, solve(t));
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2684 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1344 ms 53992 KB Output is correct
2 Correct 1351 ms 53556 KB Output is correct
3 Correct 845 ms 41928 KB Output is correct
4 Correct 86 ms 11304 KB Output is correct
5 Correct 580 ms 46040 KB Output is correct
6 Correct 77 ms 11204 KB Output is correct
7 Correct 8 ms 3156 KB Output is correct
8 Correct 4 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 3256 KB Output is correct
2 Correct 15 ms 3316 KB Output is correct
3 Correct 11 ms 3244 KB Output is correct
4 Correct 9 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3761 ms 68672 KB Output is correct
2 Correct 3030 ms 67916 KB Output is correct
3 Correct 2444 ms 61408 KB Output is correct
4 Correct 1833 ms 41360 KB Output is correct
5 Correct 343 ms 20680 KB Output is correct
6 Correct 85 ms 12952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6067 ms 91424 KB Time limit exceeded
2 Halted 0 ms 0 KB -