Submission #310678

#TimeUsernameProblemLanguageResultExecution timeMemory
310678MrDominoCities (BOI16_cities)C++14
74 / 100
6051 ms43128 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=(int) 1e5; const int K=5; const ll INF=(ll) 1e18; int n,k,m; struct E { int x; ll cost; }; vector<E> g[N]; ll dp[1<<K][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); /** Azi am terminat de citit "Enigma Otiliei" de G Calinescu -> nu-mi place finalul. **/ cin>>n>>k>>m; for (int i=1;i<(1<<k);i++) for (int j=0;j<n;j++) dp[i][j]=INF; for (int i=0;i<k;i++) { int c; cin>>c; c--; dp[1<<i][c]=0; } for (int i=0;i<m;i++) { int x,y,c; cin>>x>>y>>c; x--; y--; g[x].push_back({y,c}); g[y].push_back({x,c}); } for (int m=1;m<(1<<k);m++) { for (int m2=0;m2<=m;m2++) { int m3=m-m2; if ((m2&m3)==0) for (int i=0;i<n;i++) dp[m][i]=min(dp[m][i],dp[m2][i]+dp[m3][i]); } set<pair<ll,int>> s; for (int i=0;i<n;i++) s.insert(make_pair(dp[m][i],i)); while (!s.empty()) { int i=s.begin()->second; s.erase(s.begin()); for (auto &it : g[i]) { int j=it.x; ll val=dp[m][i]+it.cost; if (val<dp[m][j]) { s.erase({dp[m][j],j}); dp[m][j]=val; s.insert({dp[m][j],j}); } } } } ll sol=INF; for (int i=0;i<n;i++) sol=min(sol,dp[(1<<k)-1][i]); cout<<sol<<"\n"; }
#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...