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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e18;
const int N = 1e5;
int n, m, k;
vector<pair<int, ll>> graph[N];
bitset<N> bl;
void dijkstra(ll* dis){
bl.reset();
priority_queue<pair<ll, int>> pq;
for(int s = 0; s < n; s++) if(dis[s] < inf) pq.push({-dis[s], s});
while(!pq.empty()){
int nd = pq.top().second;
pq.pop();
if(bl[nd]) continue;
bl[nd] = 1;
for(auto [x, w]: graph[nd]) if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
}
int ic[5];
ll dis[32][N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> m;
for(int i = 0; i < k; i++) cin >> ic[i], ic[i]--;
for(int i = 0; i < m; i++){
int a, b; ll w; cin >> a >> b >> w; a--, b--;
graph[a].push_back({b, w});
graph[b].push_back({a, w});
}
for(int i = 0; i < k; i++){
fill(dis[1<<i], dis[1<<i]+n, inf);
dis[1<<i][ic[i]] = 0;
dijkstra(dis[1<<i]);
}
for(int mask = 1; mask < (1<<k); mask++){
if(__builtin_popcount(mask) < 2) continue;
fill(dis[mask], dis[mask]+n, inf);
for(int s = 0; s < k; s++) if(mask&(1<<s)) {
for(int i = 0; i < n; i++)
dis[mask][i] = min(dis[mask][i], dis[1<<s][i]+dis[mask^(1<<s)][i]);
}
dijkstra(dis[mask]);
}
cout << *min_element(dis[(1<<k)-1], dis[(1<<k)-1]+n) << "\n";
}
# | 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... |