답안 #968844

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968844 2024-04-24T07:32:18 Z vjudge1 Cities (BOI16_cities) C++17
100 / 100
3530 ms 48400 KB
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
//#include <grader.h>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
#define db double
using namespace std;
 
const int N = 1e5 + 10;
 
int n, k, m;
ll dp[N][32];
int city[5];
vector<pll> adj[N];
priority_queue<pll> pq;
 
void solve() {
    int n, k, m;cin >> n >> k >> m;
    for (int s = 0; s < (1<<k); ++s) for (int i = 1; i <= n; ++i) dp[i][s] = 1e18;
    for (int i = 0, u; i < k; ++i) {
        cin >> u;
        dp[u][(1<<i)] = 0;
    }
    for (int i = 1; i <= m; ++i) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].pb({v, w}); adj[v].pb({u, w});
    }
 
    for (int s = 1; s < (1<<k); ++s) {
        for (int j = 0; j < k; ++j) {
            if (s&(1<<j))
                for (int i = 1; i <= n; ++i)
                dp[i][s] = min(dp[i][s], dp[i][(1<<j)] + dp[i][s^(1<<j)]);
        }
        for (int i = 1; i <= n; ++i) pq.push({-dp[i][s], i});
        while (!pq.empty()) {
            int a = pq.top().nd; pq.pop();
            for (auto [b, w] : adj[a]) {
                if (dp[b][s] > dp[a][s] + w) {
                    dp[b][s] = dp[a][s] + w;
                    pq.push({-dp[b][s], b});
                }
            }
        }
    }
 
    ll ans = LLONG_MAX;
    for (int i = 1; i <= n; ++i) ans = min(ans, dp[i][(1<<k)-1]);
    cout << ans << '\n';
}
 
int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 2 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 825 ms 43300 KB Output is correct
2 Correct 731 ms 46908 KB Output is correct
3 Correct 479 ms 37252 KB Output is correct
4 Correct 65 ms 17084 KB Output is correct
5 Correct 401 ms 48400 KB Output is correct
6 Correct 59 ms 16976 KB Output is correct
7 Correct 3 ms 4696 KB Output is correct
8 Correct 3 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4700 KB Output is correct
2 Correct 6 ms 4832 KB Output is correct
3 Correct 4 ms 4700 KB Output is correct
4 Correct 4 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1654 ms 43208 KB Output is correct
2 Correct 1531 ms 47160 KB Output is correct
3 Correct 971 ms 37316 KB Output is correct
4 Correct 854 ms 33736 KB Output is correct
5 Correct 157 ms 20708 KB Output is correct
6 Correct 71 ms 19420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3338 ms 43980 KB Output is correct
2 Correct 3530 ms 47820 KB Output is correct
3 Correct 3039 ms 46788 KB Output is correct
4 Correct 1978 ms 37372 KB Output is correct
5 Correct 1605 ms 33764 KB Output is correct
6 Correct 282 ms 20684 KB Output is correct
7 Correct 99 ms 19676 KB Output is correct