Submission #1314103

#TimeUsernameProblemLanguageResultExecution timeMemory
1314103huypham2009Cities (BOI16_cities)C++20
100 / 100
1559 ms41432 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn=1e5+5; const int inf=1e18; int n,m,k,dp[40][maxn],imcity[10]; vector<pair<int,int>>g[maxn]; int MASK(int x) { return 1<<x; } int fullmask(int x) { return (1<<x)-1; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("cities.inp","r",stdin); freopen("cities.out","w",stdout); cin>>n>>k>>m; for(int i=1;i<=k;i++) cin>>imcity[i]; for(int i=1;i<=m;i++) { int u,v,w; cin>>u>>v>>w; g[u].push_back({v,w}); g[v].push_back({u,w}); } for(int i=1;i<=fullmask(k);i++) { for(int j=1;j<=n;j++) { dp[i][j]=inf; } } priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq; for(int i=1;i<=k;i++) { int curmask=MASK(i-1); dp[curmask][imcity[i]]=0; pq.push({0,imcity[i]}); while(pq.size()) { pair<int,int>res=pq.top(); pq.pop(); int u=res.second,w=res.first; if(w>dp[curmask][u]) continue; for(pair<int,int> v:g[u]) { if(dp[curmask][v.first]>w+v.second) { dp[curmask][v.first]=w+v.second; pq.push({dp[curmask][v.first],v.first}); } } } } // cout<<MASK(k); for(int mask=1;mask<MASK(k);mask++) { for(int submask=mask;submask>0;submask=(submask-1)&mask) { int other=mask^submask; for(int j=1;j<=n;j++) { if(dp[submask][j]+dp[other][j]<dp[mask][j]) { dp[mask][j]=dp[submask][j]+dp[other][j]; } } } for(int j=1;j<=n;j++) { if(dp[mask][j]!=inf) { pq.push({dp[mask][j],j}); } } while(pq.size()) { pair<int,int>res=pq.top(); pq.pop(); int u=res.second,w=res.first; if(w>dp[mask][u]) continue; for(pair<int,int> v:g[u]) { if(dp[mask][v.first]>w+v.second) { dp[mask][v.first]=w+v.second; pq.push({dp[mask][v.first],v.first}); } } } } int ans=inf; for(int i=1;i<=n;i++) { // cout<<dp[fullmask(k)][i]<<' '; ans=min(ans,dp[fullmask(k)][i]); } cout<<ans; return 0; }
#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...