답안 #942897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
942897 2024-03-11T06:36:42 Z Darren0724 Cities (BOI16_cities) C++17
100 / 100
1921 ms 86108 KB
#pragma GCC optimize("Ofast","O3","unroll-loops")
#pragma GCC target("avx","avx2")
#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long 
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=200005;
const ll INF=1e18;
int n,k,m;
vector<int> s;
vector<pair<int,ll>> adj[N];
vector dis(32,vector<ll>(N,INF));
vector dis1(5,vector<ll>(N,INF));
void dijk(int s,vector<ll> &dis){
    priority_queue<pair<ll,int>> pq;
    if(s!=-1){
        dis[s]=0;
    }
    for(int i=1;i<=n;i++){
        pq.push({-dis[i],i});
    }
    while(pq.size()){
        auto [dis1,a]=pq.top();
        pq.pop();
        dis1=-dis1;
        if(dis1!=dis[a])continue;
        for(auto [c,d]:adj[a]){
            ll cost=dis1+d;
            if(dis[c]>cost){
                dis[c]=cost;
                pq.push({-dis[c],c});
            }
        }
    }
}
int32_t main() {
    LCBorz;
    cin>>n>>k>>m;    
    for(int i=0;i<k;i++){
        int p;cin>>p;
        s.push_back(p);
    }
    for(int i=0;i<m;i++){
        ll a,b,c;cin>>a>>b>>c;
        adj[a].push_back({b,c});
        adj[b].push_back({a,c});
    }
    for(int i=1;i<=n;i++){
        dis[0][i]=0;
    }
    for(int i=0;i<k;i++){
        dijk(s[i],dis1[i]);
    }
    for(int mask=1;mask<(1<<k);mask++){
        for(int j=0;j<k;j++){
            if(mask&(1<<j)){
                for(int i=1;i<=n;i++){
                    dis[mask][i]=min(dis[mask][i],dis[mask^(1<<j)][i]+dis1[j][i]);
                }
            }
        }
        dijk(-1,dis[mask]);
    }
    ll ans=INF;
    for(int i=1;i<=n;i++){
        ans=min(ans,dis[(1<<k)-1][i]);
    }
    cout<<ans<<endl;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 64532 KB Output is correct
2 Correct 27 ms 64576 KB Output is correct
3 Correct 30 ms 64564 KB Output is correct
4 Correct 28 ms 64572 KB Output is correct
5 Correct 27 ms 64492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 543 ms 85724 KB Output is correct
2 Correct 507 ms 83928 KB Output is correct
3 Correct 358 ms 74260 KB Output is correct
4 Correct 74 ms 75792 KB Output is correct
5 Correct 310 ms 84692 KB Output is correct
6 Correct 73 ms 75572 KB Output is correct
7 Correct 34 ms 64576 KB Output is correct
8 Correct 28 ms 64564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 64568 KB Output is correct
2 Correct 31 ms 64564 KB Output is correct
3 Correct 30 ms 64572 KB Output is correct
4 Correct 32 ms 64576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1032 ms 86064 KB Output is correct
2 Correct 918 ms 84716 KB Output is correct
3 Correct 629 ms 74416 KB Output is correct
4 Correct 557 ms 81648 KB Output is correct
5 Correct 154 ms 77868 KB Output is correct
6 Correct 79 ms 78136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1774 ms 85060 KB Output is correct
2 Correct 1921 ms 86108 KB Output is correct
3 Correct 1784 ms 83904 KB Output is correct
4 Correct 1149 ms 74388 KB Output is correct
5 Correct 950 ms 81688 KB Output is correct
6 Correct 224 ms 77876 KB Output is correct
7 Correct 88 ms 78128 KB Output is correct