Submission #564031

#TimeUsernameProblemLanguageResultExecution timeMemory
564031perchutsCities (BOI16_cities)C++17
100 / 100
1822 ms44380 KiB
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() #define sz(x) (int) x.size() #define endl '\n' #define pb push_back #define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; using ll = long long; using ull = unsigned long long; using ii = pair<int,int>; using iii = tuple<int,int,int>; const ll inf = 1e18; const int mod = 1e9+7; const int maxn = 1e5+100; template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; } template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; } int a[5],n,k,m; vector<pair<ll,int>>g[maxn]; ll dist[1<<5][maxn]; priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>>pq; void dijkstra(ll d[maxn]){ for(int i=1;i<=n;++i)pq.push({d[i],i}); while(!pq.empty()){ pair<ll,int> me = pq.top();pq.pop(); int u = me.second; if(me.first>d[u])continue; for(pair<ll,int>e :g[u]){ if(d[e.second]>e.first+me.first){ d[e.second] = e.first + me.first; pq.push({d[e.second],e.second}); } } } } int main(){_ cin>>n>>k>>m; for(int i=0;i<k;++i){ memset(dist[1<<i], 0x3F, 8*(n+1)); cin>>a[i], dist[1<<i][a[i]] = 0; } for(int i=1;i<=m;++i){ ll c;int u,v;cin>>u>>v>>c; g[u].pb({c,v}); g[v].pb({c,u}); } for(int i=1;i<(1<<k);++i){ if(__builtin_popcount(i)>1){ memset(dist[i], 0x3F, 8*(n+10)); for(int j=0;j<k;++j) if(i>>j&1) for(int l=1;l<=n;++l) ckmin(dist[i][l],dist[i^1<<j][l]+dist[1<<j][l]); } dijkstra(dist[i]); } ll ans = inf; for(int i=1;i<=n;++i)ckmin(ans,dist[(1<<k)-1][i]); 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...