답안 #872161

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
872161 2023-11-12T11:59:14 Z nguyentunglam Cities (BOI16_cities) C++17
100 / 100
1470 ms 46228 KB
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;

const int N = 1e5 + 10;

long long f[1 << 5][N];

vector<pair<int, int> > adj[N];

int32_t main() {
  #define task ""

  cin.tie(0) -> sync_with_stdio(0);

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

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

  int n, k, m; cin >> n >> k >> m;

  memset(f, 61, sizeof(f));
  
  for(int i = 0; i < k; i++) {
    int x; cin >> x;
    f[1 << i][x] = 0;
  }

  while (m--) {
    int a, b, c; cin >> a >> b >> c;
    adj[a].emplace_back(b, c);
    adj[b].emplace_back(a, c);
  }



  for(int mask = 1; mask < (1 << k); mask++) {
    #define ii pair<long long, int> 
    priority_queue<ii, vector<ii>, greater<ii> > q;
    for(int i = 1; i <= n; i++) {
      for(int submask = mask; submask > 0; submask = submask - 1 & mask) {
        f[mask][i] = min(f[mask][i], f[submask][i] + f[mask ^ submask][i]);
      }
      q.push({f[mask][i], i});
    }

    while (!q.empty()) {
      long long cost; int u; tie(cost, u) = q.top(); q.pop();
      if (f[mask][u] != cost) continue;
      for(auto &[v, w] : adj[u]) if (f[mask][v] > cost + w) q.push({f[mask][v] = cost + w, v});
    }
  }
  long long ans = 1e18;
  for(int i = 1; i <= n; i++) ans = min(ans, f[(1 << k) - 1][i]);
  cout << ans;
}


Compilation message

cities.cpp: In function 'int32_t main()':
cities.cpp:48:62: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   48 |       for(int submask = mask; submask > 0; submask = submask - 1 & mask) {
      |                                                      ~~~~~~~~^~~
cities.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |     freopen("task.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     freopen("task.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:23:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |     freopen (task".inp", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:24:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |     freopen (task".out", "w", stdout);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 27740 KB Output is correct
2 Correct 4 ms 27740 KB Output is correct
3 Correct 4 ms 27740 KB Output is correct
4 Correct 4 ms 27740 KB Output is correct
5 Correct 4 ms 27740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 367 ms 44860 KB Output is correct
2 Correct 365 ms 45408 KB Output is correct
3 Correct 218 ms 36664 KB Output is correct
4 Correct 48 ms 36000 KB Output is correct
5 Correct 200 ms 44952 KB Output is correct
6 Correct 44 ms 35896 KB Output is correct
7 Correct 6 ms 27996 KB Output is correct
8 Correct 5 ms 27996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 27996 KB Output is correct
2 Correct 8 ms 27996 KB Output is correct
3 Correct 7 ms 27940 KB Output is correct
4 Correct 6 ms 27996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 759 ms 45832 KB Output is correct
2 Correct 708 ms 45192 KB Output is correct
3 Correct 453 ms 36732 KB Output is correct
4 Correct 391 ms 41432 KB Output is correct
5 Correct 120 ms 38140 KB Output is correct
6 Correct 50 ms 37740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1461 ms 45196 KB Output is correct
2 Correct 1470 ms 46228 KB Output is correct
3 Correct 1458 ms 45868 KB Output is correct
4 Correct 899 ms 37344 KB Output is correct
5 Correct 719 ms 41456 KB Output is correct
6 Correct 173 ms 38044 KB Output is correct
7 Correct 63 ms 37680 KB Output is correct