# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
37327 | szawinis | Cities (BOI16_cities) | C++14 | 2523 ms | 41116 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.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
const int N = 1e5+1;
const ll INF = 1e17+1;
int n, k, m;
vector<int> s;
vector<pair<int,int> > g[N];
ll ans = INF, d[5][N], d2[1 << 5][N], res[1 << 5];
priority_queue<pli, vector<pli>, greater<pli> > pq;
int main() {
scanf("%d %d %d", &n, &k, &m);
for(int i = 0, v; i < k; i++) {
scanf("%d", &v);
s.push_back(v-1);
}
for(int i = 0, u, v, w; i < m; i++) {
scanf("%d %d %d", &u, &v, &w);
g[u-1].emplace_back(v-1, w);
g[v-1].emplace_back(u-1, w);
}
for(int i = 0; i < k; i++) {
fill(d[i], d[i]+n, INF);
d[i][s[i]] = 0;
pq.emplace(0, s[i]);
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
for(auto p: g[u]) if(d[i][u] + p.second < d[i][p.first]) {
d[i][p.first] = d[i][u] + p.second;
pq.emplace(d[i][p.first], p.first);
}
}
}
for(int mask = 0; mask < 1 << k; mask++) {
for(int v = 0; v < n; v++) d2[mask][v] = INF;
res[mask] = INF;
}
for(int i = 0; i < k; i++) d2[1 << i][s[i]] = res[1 << i] = 0;
for(int mask = 1; mask < (1 << k)-1; mask++) {// chec klimits
for(int v = 0; v < n; v++) if(!d2[mask][v]) pq.emplace(0, v);
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
for(auto p: g[u]) if(d2[mask][u] + p.second < d2[mask][p.first]) {
d2[mask][p.first] = d2[mask][u] + p.second;
pq.emplace(d2[mask][p.first], p.first);
}
}
for(int i = 0; i < k; i++) if(!(mask >> i & 1)) {
int new_mask = mask | 1 << i;
for(int v = 0; v < n; v++) {
if(!d2[mask][v] || d2[mask][v] + d[i][v] == d2[mask][s[i]])
d2[new_mask][v] = 0;
}
res[new_mask] = min(res[mask] + d2[mask][s[i]], res[new_mask]);
}
}
printf("%lld", res[(1 << k) - 1]);
}
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... |