Submission #1252218

#TimeUsernameProblemLanguageResultExecution timeMemory
1252218magic_tripCities (BOI16_cities)C++20
52 / 100
871 ms31544 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[5][5][101010]; 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 i, ll j){ fill(dist2[i][j], dist2[i][j]+101010,Lnf); priority_queue<array<ll,2>,vector<array<ll,2>>,greater<array<ll,2>>> pq; for(int t = 1 ; t <= n ; t++){ dist2[i][j][t] = dist[i][t] + dist[j][t]; pq.push({dist2[i][j][t],t}); } while(sz(pq)){ auto [d,cur] = pq.top(); pq.pop(); if(d!=dist2[i][j][cur])continue; for(auto [next,w] : adj[cur])if(dist2[i][j][next] > d+w){ dist2[i][j][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); for(int i = 0 ; i < k ; i++)for(int j = i+1 ; j < k ; j++)get_dist2(i,j); 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){ for(int u = 1 ; u <= n ; u++){ for(int i = 0 ; i < (1<<k) ; i++)if(__builtin_popcount(i)==2){ vector<ll> a, b; for(int j = 0 ; j < k ; j++){ if(i&(1<<j))a.push_back(j); else b.push_back(j); } ans=min(ans,dist2[a[0]][a[1]][u] + dist2[b[0]][b[1]][u]); } } cout<<ans; exit(0); } for(int u = 1 ; u <= n ; u++){ for(int i = 0 ; i < (1<<k) ; i++)if(__builtin_popcount(i)==2){ vector<ll> a, b; for(int j = 0 ; j < k ; j++){ if(i&(1<<j))a.push_back(j); else b.push_back(j); } ll s = dist2[a[0]][a[1]][u]; for(int j = 0 ; j < (1<<k-2) ; j++)if(__builtin_popcount(j)==1){ vector<ll> c,d; for(int x = 0 ; x < k-2 ; x++){ if(j&(1<<x))c.push_back(x); else d.push_back(x); } ans = min(ans,s+dist[c[0]][u]+dist2[d[0]][d[1]][u]); } } } cout<<ans; }
#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...