제출 #1130977

#제출 시각아이디문제언어결과실행 시간메모리
1130977RandomUserCities (BOI16_cities)C++20
74 / 100
6095 ms137964 KiB
#include <bits/stdc++.h>
#define ar array
using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int maxn = 1e5 + 5;

ll dist[5][maxn], dp[maxn][1<<5], imp[5];
vector<pii> G[maxn];

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

    int n, k, m; cin >> n >> k >> m;
    for(int i=0; i<k; i++) cin >> imp[i];

    for(int i=1; i<=m; i++) {
        int a, b, w; cin >> a >> b >> w;
        G[a].push_back({ b, w });
        G[b].push_back({ a, w });
    }

    for(int i=0; i<k; i++) {
        priority_queue<pll, vector<pll>, greater<> > pq;
        for(int j=1; j<=n; j++) dist[i][j] = 1e18;
        pq.push({ 0, imp[i] }); dist[i][imp[i]] = 0;
        while(!pq.empty()) {
            auto [d, u] = pq.top(); pq.pop();
            for(auto [v, w] : G[u])
                if(dist[i][v] > d + w) pq.push({ dist[i][v] = d + w, v });
        }
    }

    priority_queue<ar<ll, 3>, vector<ar<ll, 3> >, greater<> > pq;
    for(int i=1; i<=n; i++)
        for(int j=0; j<(1<<k); j++) dp[i][j] = 1e18;
    for(int i=0; i<k; i++) pq.push({ dp[imp[i]][1<<i] = 0, imp[i], 1<<i });

    while(!pq.empty()) {
        auto [d, u, s] = pq.top(); pq.pop();
        if(d != dp[u][s]) continue;
        for(auto [v, w] : G[u])
            if(dp[v][s] > d + w) pq.push({ dp[v][s] = d + w, v, s });
        for(int i=0; i<k; i++)
            if(dp[u][s|(1<<i)] > d + dist[i][u])
                pq.push({ dp[u][s|(1<<i)] = d + dist[i][u], u, s|(1<<i) });
    }

    ll ans = 1e18;
    for(int i=1; i<=n; i++) ans = min(ans, dp[i][(1<<k)-1]);
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...