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 ll long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define ALL(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define SZ(x) (int)x.size()
#define CASE int t;cin>>t;for(int ca=1;ca<=t;ca++)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int MAX = 1 << 20, MOD = 1e9 + 7;
ll dp[MAX][32];
vector<pll> edge[MAX];
void solve(){
int n, k, m;
cin >> n >> k >> m;
for(int i = 1;i <= n;i++)
for(int j = 0;j < (1 << k);j++)
dp[i][j] = 2e18;
for(int i = 0;i < k;i++){
int x;
cin >> x;
dp[x][1 << i] = 0;
}
for(int i = 1;i <= m;i++){
int u, v, w;
cin >> u >> v >> w;
edge[u].PB(MP(v, w));
edge[v].PB(MP(u, w));
}
for(int i = 1;i < (1 << k);i++){
for(int j = 0;j < k;j++){
if(i >> j & 1)
for(int l = 1;l <= n;l++)
dp[l][i] = min(dp[l][i], dp[l][1 << j] + dp[l][i ^ (1 << j)]);
}
priority_queue<pll> pq;
for(int j = 1;j <= n;j++)
pq.push(MP(-dp[j][i], j));
while(!pq.empty()){
auto now = pq.top();
pq.pop();
now.F = -now.F;
if(now.F != dp[now.S][i])
continue;
for(auto j : edge[now.S]){
if(now.F + j.S < dp[j.F][i]){
dp[j.F][i] = now.F + j.S;
pq.push(MP(-dp[j.F][i], j.F));
}
}
}
}
ll ans = 2e18;
for(int i = 1;i <= n;i++)
ans = min(ans, dp[i][(1 << k) - 1]);
cout << ans << '\n';
}
int main(){
IOS
// CASE
solve();
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... |