답안 #919173

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
919173 2024-01-31T12:24:26 Z phong Cities (BOI16_cities) C++17
100 / 100
3004 ms 47608 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>

#define ll long long
const int nmax = 1e5 + 5, dmax = 2e5 + 5;
const ll oo = 1e18, base = 311;
const int lg = 18, M =2e2 + 5;
const ll mod = 1e9 + 7;
#define pii pair<ll, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
using namespace std;

int n, k, m, a[nmax];
ll dp[1 << 5][nmax];
vector<pii> adj[nmax];

priority_queue<pii, vector<pii>, greater<pii>> q;
void bfs(int msk){
    for(int i = 1; i <= n; ++i){
        q.push({dp[msk][i], i});
    }

    while(q.size()){
        pii tmp = q.top();
        int u = tmp.se;
        q.pop();
        if(tmp.fi != dp[msk][u]) continue;
        for(auto [v, w] : adj[u]){
            if(dp[msk][u] + w < dp[msk][v]){
                dp[msk][v] = dp[msk][u] + w;
                q.push({dp[msk][v], v});
            }
        }
    }
}

void ckmin(ll &a, ll b){
    a = min(a, b);
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n >> k >> m;
    memset(dp, 0x3f, sizeof dp);

    for(int i = 0; i < k; ++i){
        cin >> a[i];
        dp[1 << i][a[i]] = 0;
    }
    for(int i = 1, u, v, c; i <= m; ++i){
        cin >> u >> v >> c;
        adj[u].push_back({v, c});
        adj[v].push_back({u, c});
    }
    for(int msk = 1; msk < (1 << k); ++msk){
        bfs(msk);
        for(int x = 1; x <= n; ++x){
            for(int i = 0; i < k; ++i){
                if(msk >> i & 1){
                    ckmin(dp[msk][x], dp[msk ^ (1 << i)][x] + dp[1 << i][x]);
                }
            }
        }
        bfs(msk);

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

}
/*
5 2
3 2
2 5
3 4
3 1

*/

Compilation message

cities.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   44 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27736 KB Output is correct
2 Correct 6 ms 27736 KB Output is correct
3 Correct 6 ms 27788 KB Output is correct
4 Correct 7 ms 27744 KB Output is correct
5 Correct 6 ms 27736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 697 ms 46812 KB Output is correct
2 Correct 825 ms 45620 KB Output is correct
3 Correct 457 ms 37240 KB Output is correct
4 Correct 67 ms 40152 KB Output is correct
5 Correct 427 ms 47608 KB Output is correct
6 Correct 59 ms 40108 KB Output is correct
7 Correct 9 ms 27992 KB Output is correct
8 Correct 7 ms 27992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 27992 KB Output is correct
2 Correct 11 ms 27992 KB Output is correct
3 Correct 11 ms 27736 KB Output is correct
4 Correct 10 ms 27992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1619 ms 46860 KB Output is correct
2 Correct 1622 ms 46120 KB Output is correct
3 Correct 1223 ms 37320 KB Output is correct
4 Correct 739 ms 44524 KB Output is correct
5 Correct 162 ms 41716 KB Output is correct
6 Correct 67 ms 42532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3004 ms 47112 KB Output is correct
2 Correct 2989 ms 47380 KB Output is correct
3 Correct 2980 ms 46492 KB Output is correct
4 Correct 1969 ms 37332 KB Output is correct
5 Correct 1561 ms 44828 KB Output is correct
6 Correct 287 ms 42184 KB Output is correct
7 Correct 81 ms 42868 KB Output is correct