답안 #1075497

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1075497 2024-08-26T07:05:25 Z nathan4690 Cities (BOI16_cities) C++14
100 / 100
1476 ms 48960 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 2e5+5, inf=1e18;

ll n,k,m,dist[6][6], im[6], d[maxn],dp[(1 << 5)][maxn];
vector<pair<ll,ll>> G[maxn];
priority_queue<pll, vector<pll>, greater<pll>> pq;
bool vis[maxn];

void dijkstra(ll s){
    f1(i,n) d[i] = inf;
    d[s] = 0ll;
    pq.push({d[s], s});
    while(!pq.empty()){
        pair<ll,ll> item = pq.top();
        pq.pop();
        ll dist = item.first, u = item.second;
        if(dist != d[u]) continue;
        vis[u] = true;
        for(pair<ll,ll> e: G[u]){
            ll c = e.first, w = e.second;
            if(d[u] + w < d[c]){
                d[c] = d[u] + w;
                pq.push({d[c], c});
            }
        }
    }
}

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

    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n >> k >> m;
    for(ll i=0;i<(1 << k);i++){
        f1(u, n) dp[i][u] = inf;
    }
    f1(i,k) {
        cin >> im[i];
        dp[1 << (i-1)][im[i]] = 0;
    }
    f1(i,m){
        ll u, v, w; cin >> u >> v >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
    }
    for(ll mask=1;mask<(1 << k);mask++){
        // if(__builtin_popcount(mask) == 1) continue;
        for(ll submask=mask&(mask-1);submask;submask=mask&(submask-1)){
            f1(u, n){
                dp[mask][u] = min(dp[mask][u], dp[submask][u] + dp[mask ^ submask][u]);
            }
        }
        f1(u,n) pq.push({dp[mask][u], u});
        while(!pq.empty()){
            pair<ll,ll> item = pq.top();
            pq.pop();
            ll dist = item.first, u = item.second;
            // cout << dist << ' ' << u << ' ' << mask << ' ' << dp[mask][u];el;
            if(dist > dp[mask][u]) continue;
            // vis[u] = true;
            for(pair<ll,ll> e: G[u]){
                ll c = e.first, w = e.second;
                if(dp[mask][u] + w < dp[mask][c]){
                    dp[mask][c] = dp[mask][u] + w;
                    pq.push({dp[mask][c], c});
                }
            }
        }
    }
    cout << *min_element(dp[(1 << k) - 1]+1, dp[(1 << k) - 1] + n + 1);
    return 0;
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(__file_name ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4980 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 356 ms 30116 KB Output is correct
2 Correct 340 ms 28992 KB Output is correct
3 Correct 206 ms 20936 KB Output is correct
4 Correct 43 ms 17552 KB Output is correct
5 Correct 194 ms 26736 KB Output is correct
6 Correct 43 ms 17492 KB Output is correct
7 Correct 4 ms 5468 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5468 KB Output is correct
2 Correct 6 ms 5468 KB Output is correct
3 Correct 4 ms 5468 KB Output is correct
4 Correct 4 ms 5468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 682 ms 36348 KB Output is correct
2 Correct 699 ms 35324 KB Output is correct
3 Correct 436 ms 27080 KB Output is correct
4 Correct 398 ms 28360 KB Output is correct
5 Correct 114 ms 20080 KB Output is correct
6 Correct 49 ms 20308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1347 ms 48960 KB Output is correct
2 Correct 1353 ms 48836 KB Output is correct
3 Correct 1476 ms 48060 KB Output is correct
4 Correct 1028 ms 39864 KB Output is correct
5 Correct 796 ms 34700 KB Output is correct
6 Correct 216 ms 21868 KB Output is correct
7 Correct 55 ms 20308 KB Output is correct