# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
310327 | BeanZ | Cities (BOI16_cities) | C++14 | 2513 ms | 44868 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I_Love_LPL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
ll dp[(1 << 5) + 1][N], p[N];
vector<pair<ll, ll>> node[N];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("A.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll n, k, m;
cin >> n >> k >> m;
for (int i = 0; i < (1 << k); i++){
for (int j = 1; j <= n; j++){
dp[i][j] = 1e18;
}
}
for (int i = 0; i < k; i++) cin >> p[i], dp[(1 << i)][p[i]] = 0;
for (int i = 1; i <= m; i++){
ll u, v, c;
cin >> u >> v >> c;
node[u].push_back({v, c});
node[v].push_back({u, c});
}
for (int i = 1; i < (1 << k); i++){
ll now = -1;
for (int j = 0; j < k; j++){
if (!(i & (1 << j))) continue;
now = j;
break;
}
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
for (int j = 1; j <= n; j++){
if (now != -1) dp[i][j] = min(dp[i][j], dp[i ^ (1 << now)][j] + dp[(1 << now)][j]);
pq.push({dp[i][j], j});
}
while (pq.size()){
pair<ll, ll> x = pq.top();
pq.pop();
if (x.first != dp[i][x.second]) continue;
for (auto j : node[x.second]){
if ((x.first + j.second) < dp[i][j.first]){
dp[i][j.first] = x.first + j.second;
pq.push({dp[i][j.first], j.first});
}
}
}
}
ll ans = 1e18;
for (int i = 1; i <= n; i++){
ans = min(ans, dp[(1 << k) - 1][i]);
}
cout << ans;
}
/*
*/
Compilation message (stderr)
# | 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... |