Submission #1058941

# Submission time Handle Problem Language Result Execution time Memory
1058941 2024-08-14T15:13:51 Z vjudge1 Cities (BOI16_cities) C++17
100 / 100
5294 ms 72892 KB
#include <bits/stdc++.h>

using namespace std;

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

  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";
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 55896 KB Output is correct
2 Correct 21 ms 56048 KB Output is correct
3 Correct 21 ms 55896 KB Output is correct
4 Correct 15 ms 55900 KB Output is correct
5 Correct 14 ms 55872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 677 ms 68808 KB Output is correct
2 Correct 910 ms 68156 KB Output is correct
3 Correct 384 ms 62576 KB Output is correct
4 Correct 60 ms 63012 KB Output is correct
5 Correct 315 ms 68548 KB Output is correct
6 Correct 52 ms 62804 KB Output is correct
7 Correct 23 ms 56028 KB Output is correct
8 Correct 18 ms 56156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 56152 KB Output is correct
2 Correct 23 ms 56152 KB Output is correct
3 Correct 19 ms 55900 KB Output is correct
4 Correct 20 ms 56152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2029 ms 68900 KB Output is correct
2 Correct 2217 ms 72400 KB Output is correct
3 Correct 1176 ms 62404 KB Output is correct
4 Correct 934 ms 68084 KB Output is correct
5 Correct 213 ms 66916 KB Output is correct
6 Correct 73 ms 64852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4528 ms 72884 KB Output is correct
2 Correct 4723 ms 72892 KB Output is correct
3 Correct 5294 ms 70208 KB Output is correct
4 Correct 2934 ms 61116 KB Output is correct
5 Correct 1751 ms 65984 KB Output is correct
6 Correct 545 ms 64800 KB Output is correct
7 Correct 132 ms 63360 KB Output is correct