Submission #894668

#TimeUsernameProblemLanguageResultExecution timeMemory
894668LCJLYCities (BOI16_cities)C++14
100 / 100
1627 ms51944 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define ld long double #define show(x,y) cout << y << " " << #x << endl; #define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl; #define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl; #define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl; typedef pair<int,int>pii; //typedef pair<pii,pii>pi2; typedef array<pii,5>pi2; void solve(){ int n,k,m; cin >> n >> k >> m; int city[k]; for(int x=0;x<k;x++){ cin >> city[x]; } vector<pii>adj[n+5]; int temp,temp2,temp3; for(int x=0;x<m;x++){ cin >> temp >> temp2 >> temp3; adj[temp].push_back({temp2,temp3}); adj[temp2].push_back({temp,temp3}); } int dist[35][n+5]; memset(dist,63,sizeof(dist)); for(int x=0;x<k;x++){ dist[1<<x][city[x]]=0; } for(int mask=1;mask<(1<<k);mask++){ //find submask for(int y=1;y<mask;y++){ if((y|mask)!=mask) continue; int hold=mask^y; for(int i=1;i<=n;i++){ dist[mask][i]=min(dist[mask][i],dist[y][i]+dist[hold][i]); } } priority_queue<pii,vector<pii>,greater<pii>>pq; for(int y=1;y<=n;y++){ if(dist[mask][y]<1e16){ pq.push({dist[mask][y],y}); } } while(!pq.empty()){ pii cur=pq.top(); pq.pop(); int index=cur.second; int d=cur.first; if(dist[mask][index]!=d) continue; for(auto it:adj[index]){ int next=it.first; int nd=d+it.second; if(nd>=dist[mask][next]) continue; dist[mask][next]=nd; pq.push({nd,next}); } } } int best=LONG_LONG_MAX; for(int x=1;x<=n;x++){ best=min(best,dist[(1<<k)-1][x]); } cout << best; } int32_t main(){ ios::sync_with_stdio(0); cin.tie(0); //freopen("in.txt", "r", stdin); int t=1; //cin >> t; while(t--){ solve(); } }
#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...