제출 #1252213

#제출 시각아이디문제언어결과실행 시간메모리
1252213magic_tripCities (BOI16_cities)C++20
14 / 100
239 ms25496 KiB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma optimize("unroll-loops")
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) x.begin(), x.end()
#define rll(x) x.rbegin(), x.rend()
#define COMP(x) x.erase(unique(all(x)), x.end())
#define MOD 1000000007
#define MOD2 998244353
#define sz(x) (ll)x.size()
typedef __int128_t lll;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ll, pll> PP;
const ll Lnf = 2e18;
ll n, k, m;
ll dist[5][101010];
ll dist2[1010][1010];
vector<ll> imp;
vector<array<ll,2>> adj[101010];
bool chk[101010];
void get_dist(ll x, ll i){
    fill(dist[i],dist[i]+101010,Lnf);
    dist[i][x] = 0;
    priority_queue<array<ll,2>,vector<array<ll,2>>,greater<array<ll,2>>> pq;
    pq.push({0,x});
    while(sz(pq)){
        auto [d,cur] = pq.top(); pq.pop();
        if(d!=dist[i][cur])continue;
        for(auto [next,w] : adj[cur])if(dist[i][next] > d+w){
            dist[i][next] = d+w;
            pq.push({d+w,next});
        }
    }
}
void get_dist2(ll x, ll i){
    fill(dist2[i],dist2[i]+1010,Lnf);
    dist2[i][x] = 0;
    priority_queue<array<ll,2>,vector<array<ll,2>>,greater<array<ll,2>>> pq;
    pq.push({0,x});
    while(sz(pq)){
        auto [d,cur] = pq.top(); pq.pop();
        if(d!=dist2[i][cur])continue;
        for(auto [next,w] : adj[cur])if(dist2[i][next] > d+w){
            dist2[i][next] = d+w;
            pq.push({d+w,next});
        }
    }
}
int main(){
    fast;
    cin>>n>>k>>m;
    imp.resize(k);
    for(auto &i : imp)cin>>i, chk[i]=1;
    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 = 0 ; i < k ; i++)get_dist(imp[i],i);
    ll ans = Lnf;
    for(int i = 1 ; i <= n ; i++)if(!chk[i]){
        ll s = 0;
        for(int j = 0 ; j < k ; j++)s+=dist[j][i];
        ans=min(ans,s);
    }
    for(int i = 0 ; i < k ; i++){
        ll s = 0;
        for(int j = 0 ; j < k ; j++)if(i!=j)s+=dist[j][imp[i]];
        ans=min(ans,s);
    }
    if(k<=3){
        cout<<ans;
        exit(0);
    }
    if(k>4)return 0;
    for(int i = 1 ; i <= n ; i++)get_dist2(i,i);
    ll U=-1, V, I;
    for(int u = 1 ; u <= n ; u++){
        for(int v = u+1 ; v <= n ; v++){
            for(int i = 0 ; i < (1<<k) ; i++){
                if(__builtin_popcount(i) == 2){
                    ll s = dist2[u][v];
                    for(int j = 0 ; j < k ; j++){
                        if(i&(1<<j))s += dist2[u][imp[j]];
                        else s += dist2[v][imp[j]];
                    }
//                    cout<<u<<" "<<v<<" "<<s<<endl;
                    ans=min(ans,s);
                    if(ans==s){
                        U=u,V=v,I=i;
                    }
                }
            }
        }
    }
    cout<<ans;
    if(k==4 and U>=0){
        ll s = dist2[U][V];
        ll a=-1,b=-1,c=-1,d=-1;
        for(int j = 0 ; j < k ; j++){
            if(I&(1<<j)){
                s += dist2[U][imp[j]];
                if(a>=0)b=imp[j];
                else a=imp[j];
            }
            else{
                s += dist2[V][imp[j]];
                if(c>=0)d=imp[j];
                else c=imp[j];
            }
        }
        if(s==ans){
            assert(dist2[a][U] + dist2[U][V] + dist2[V][d] == dist2[a][d] or dist2[c][V] + dist2[U][V] + dist2[b][U] == dist2[b][c] or dist2[a][b] == dist2[a][U] + dist2[U][b] or dist2[c][d] == dist2[c][V] + dist2[V][d]);
        }
    }
}
#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...