This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+10;
const ll inf=1e15;
ll n,k,m;
ll spec[10];
vector<pair<ll,ll> > g[maxn];
int vel;
ll sta1[maxn];
ll sta2[maxn];
ll d[6][maxn];
ll naj[(1<<6)];
ll nenaj[(1<<6)][maxn];
void dijkstra(ll poc,ll* dist){
priority_queue<pair<ll,ll> > pq;
pq.push(pair<ll,ll>(0,poc));
for(ll i=1;i<=n;i++) dist[i]=inf;
dist[poc]=0;
while(!pq.empty()){
auto tren=pq.top();
pq.pop();
tren.first*=-1;
if(dist[tren.second]<tren.first) continue;
for(auto i:g[tren.second]){
if(dist[i.first]>tren.first+i.second) pq.push(pair<ll,ll>(-tren.first-i.second,i.first));
dist[i.first]=min(dist[i.first],tren.first+i.second);
}
}
}
int main(){
cin>>n>>k>>m;
for(ll i=0;i<k;i++) cin>>spec[i];
for(ll i=0;i<m;i++){
ll u,v,c;
cin>>u>>v>>c;
g[u].push_back({v,c});
g[v].push_back({u,c});
}
for(ll i=0;i<(1<<k);i++) for(ll j=1;j<=n;j++) nenaj[i][j]=inf;
for(ll i=0;i<k;i++) dijkstra(spec[i],nenaj[1<<i]);
for(ll mask=1;mask<(1<<k);mask++){
naj[mask]=inf;
ll mask1=mask;
while(mask1>0){
ll mask2=mask^mask1;
for(ll sred=1;sred<=n;sred++) nenaj[mask][sred]=min(nenaj[mask][sred],nenaj[mask1][sred]+nenaj[mask2][sred]);
mask1--;
mask1&=mask;
}
for(ll sred=1;sred<=n;sred++) naj[mask]=min(naj[mask],nenaj[mask][sred]);
}
/*for(ll i=0;i<k;i++){
for(ll mask=1;mask<(1<<k);mask++){
for(ll mask2=1;mask2<(1<<k);mask2++){
if((mask&mask2)==0) continue;
naj[mask|mask2]=min(naj[mask|mask2],naj[mask2]+naj[mask]);
}
}
}*/
cout<<naj[(1<<k)-1];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |