#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define s second
#define f first
#define pii pair <int,int>
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
#define int ll
using namespace std;
const int N = 2e5 + 5,inf = 1e18;
int arr[N],dist[8][N],par[N],n,cur;
bool fix[N];
int dd[105][N],ddd[N];
int DD[105][N],val[105][N];
vector <pii> v[N];
signed main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int k,m;
cin>>n>>k>>m;
for (int i = 1; i <= k; i++)
cin >> arr[i];
for (int i =1; i <= m; i++){
int a,b,c;
cin>>a>>b>>c;
v[a].pb({b,c});
v[b].pb({a,c});
}
for (int i = 1; i <= k; i++){
for (int j = 1; j <= n; j++)
dist[i][j] = inf;
priority_queue<pii> pq;
dist[i][arr[i]] = 0;
pq.push({0,arr[i]});
while (pq.size()){
int D = -pq.top().f,x = pq.top().s;
pq.pop();
if (dist[i][x] != D) continue;
for (auto [to,w]: v[x]){
if (dist[i][to] > D + w){
dist[i][to] = D + w;
pq.push({-dist[i][to],to});
}
}
}
}
int ans=inf;
for (int i = 1; i <= n; i++){
int cur=0;
for (int j = 1; j <= k; j++)
cur += dist[j][i];
ans=min(ans,cur);
}
if (k < 4){
cout<<ans<<endl;
exit(0);
}
for (int mask1=0;mask1<(1<<k); mask1++)
for(int i=1;i<=n;i++)
DD[mask1][i] = inf;
for (int mask2=0;mask2<(1<<k); mask2++){
for (int i = 1; i <= n; i++){
for (int j=0;j<k;j++){
if (((mask2>>j) & 1)) val[mask2][i] += (dist[j + 1][i]);
}
}
}
for (int mask1 = 0; mask1 < (1<<k); mask1++){
for (int i = 1; i <= n; i++){
dd[mask1][i] = 0;
for (int j = 0; j < k; j++){
if (((mask1>>j) & 1)) dd[mask1][i] += dist[j + 1][i];
}
}
priority_queue <pii> pq;
for (int i = 1; i <= n; i++)
pq.push({-dd[mask1][i],i});
while (pq.size()){
int D = -pq.top().f,x = pq.top().s;
pq.pop();
if (dd[mask1][x] != D) continue;
for (auto [to,w]: v[x]){
if (dd[mask1][to] > D + w){
dd[mask1][to] = D + w;
pq.push({-dd[mask1][to],to});
}
}
}
for (int mask2 = 0; mask2 < (1<<k); mask2++){
int y = (mask1 & mask2);
if (y > 0) continue;
for (int i = 1; i <= n; i++){
int kk = dd[mask1][i] + val[mask2][i];
DD[(mask1 | mask2)][i] = min(DD[(mask1 | mask2)][i],kk);
}
}
}
for (int mask1=0;mask1<(1<<k); mask1++){
for (int i =1 ; i <= n; i++){
ddd[i] = DD[mask1][i];
}
priority_queue <pii> pq;
for (int i = 1; i <= n; i++)
pq.push({-ddd[i],i});
while (pq.size()){
int D = -pq.top().f,x = pq.top().s;
pq.pop();
if (ddd[x] != D) continue;
for (auto [to,w]: v[x]){
if (ddd[to] > D + w){
ddd[to] = D + w;
pq.push({-ddd[to],to});
}
}
}
for (int i = 1; i <= n; i++){
for (int j =0;j < k; j++){
if (((mask1>>j) & 1)) continue;
ddd[i] += (dist[j + 1][i]);
}
ans=min(ans,ddd[i]);
}
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
6 ms |
68444 KB |
Output is correct |
5 |
Correct |
7 ms |
72796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
29076 KB |
Output is correct |
2 |
Correct |
125 ms |
29796 KB |
Output is correct |
3 |
Correct |
44 ms |
22104 KB |
Output is correct |
4 |
Correct |
38 ms |
26044 KB |
Output is correct |
5 |
Correct |
91 ms |
29036 KB |
Output is correct |
6 |
Correct |
37 ms |
23812 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
72796 KB |
Output is correct |
2 |
Correct |
13 ms |
72792 KB |
Output is correct |
3 |
Correct |
12 ms |
72536 KB |
Output is correct |
4 |
Correct |
10 ms |
72536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
980 ms |
102188 KB |
Output is correct |
2 |
Correct |
902 ms |
101192 KB |
Output is correct |
3 |
Correct |
687 ms |
94596 KB |
Output is correct |
4 |
Correct |
521 ms |
93048 KB |
Output is correct |
5 |
Correct |
146 ms |
85072 KB |
Output is correct |
6 |
Correct |
57 ms |
85608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2053 ms |
140260 KB |
Output is correct |
2 |
Correct |
2028 ms |
140128 KB |
Output is correct |
3 |
Correct |
1822 ms |
139428 KB |
Output is correct |
4 |
Correct |
1500 ms |
132476 KB |
Output is correct |
5 |
Correct |
1092 ms |
113016 KB |
Output is correct |
6 |
Correct |
237 ms |
89520 KB |
Output is correct |
7 |
Correct |
65 ms |
86608 KB |
Output is correct |