Submission #1118367

# Submission time Handle Problem Language Result Execution time Memory
1118367 2024-11-25T12:02:26 Z VinhLuu Cities (BOI16_cities) C++17
100 / 100
2847 ms 96640 KB
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()

using namespace std;

typedef pair<ll, ll> pii;

const int N = 1e6 + 5;
const int M = 1e6 + 5;
const ll oo = 1e16;

int n, k, m;
ll f[N][(1 << 6)];
int a[N], rev[N];
vector<pii> p[N];

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

  #define task "v"
  if(fopen(task ".inp","r")){
    freopen(task ".inp","r",stdin);
    freopen(task ".out","w",stdout);
  }

  cin >> n >> k >> m;
  for(int i = 1; i <= k; i ++){
    cin >> a[i]; rev[a[i]] = i;
  }
  for(int i = 1; i <= m; i ++) {
    int x, y, c; cin >> x >> y >> c;
    p[x].push_back({y, c});
    p[y].push_back({x, c});
  }
  for(int msk = 0; msk < (1 << k); msk ++) for(int i = 1; i <= n; ++ i) f[i][msk] = oo;
  for(int i = 1; i <= k; i ++) f[a[i]][(1 << (i - 1))] = 0;
  for(int msk = 1; msk < (1 << k); msk ++){
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    for(int i = 1; i <= n; i ++){
      for(int sub = msk; sub > 0; sub = (sub - 1) & msk){
        f[i][msk] = min(f[i][msk], f[i][sub] + f[i][(msk ^ sub)]);
      }
      pq.push({f[i][msk], i});
    }
    while(!pq.empty()){
      ll w, u; tie(w, u) = pq.top(); pq.pop();
      if(w > f[u][msk]) continue;
      for(auto [j, val] : p[u]) if(f[j][msk] > w + val){
        pq.push({f[j][msk] = w + val, j});
      }
    }
  }

  ll ans = oo;
  for(int i = 1; i <= n; i ++) ans = min(ans, f[i][(1 << k) - 1]);
  cout << ans;
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 23900 KB Output is correct
2 Correct 9 ms 23912 KB Output is correct
3 Correct 12 ms 24056 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 9 ms 23992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 644 ms 95188 KB Output is correct
2 Correct 669 ms 94704 KB Output is correct
3 Correct 463 ms 85020 KB Output is correct
4 Correct 64 ms 36936 KB Output is correct
5 Correct 365 ms 95032 KB Output is correct
6 Correct 59 ms 36936 KB Output is correct
7 Correct 16 ms 24656 KB Output is correct
8 Correct 17 ms 24656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24688 KB Output is correct
2 Correct 16 ms 24668 KB Output is correct
3 Correct 16 ms 24412 KB Output is correct
4 Correct 18 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1388 ms 96084 KB Output is correct
2 Correct 1387 ms 95552 KB Output is correct
3 Correct 1016 ms 85076 KB Output is correct
4 Correct 690 ms 67424 KB Output is correct
5 Correct 144 ms 43272 KB Output is correct
6 Correct 74 ms 39336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2847 ms 96640 KB Output is correct
2 Correct 2613 ms 96452 KB Output is correct
3 Correct 2731 ms 94696 KB Output is correct
4 Correct 2025 ms 85084 KB Output is correct
5 Correct 1375 ms 67540 KB Output is correct
6 Correct 211 ms 43248 KB Output is correct
7 Correct 90 ms 39100 KB Output is correct