# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
37330 | szawinis | Cities (BOI16_cities) | C++14 | 3069 ms | 41916 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;
bool mark[N];
vector<int> s;
vector<pair<int,int> > g[N];
ll ans = INF, dp[1 << 5][N];
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 mask = 1; mask < 1 << k; mask++) {
for(int v = 0; v < n; v++) dp[mask][v] = INF;
}
for(int i = 0; i < k; i++) dp[1 << i][s[i]] = 0;
for(int mask = 1; mask < 1 << k; mask++) {
for(int prev_mask1 = 1; prev_mask1 < mask; prev_mask1++) {
int prev_mask2 = prev_mask1 ^ mask;
if((prev_mask1 | mask) != mask || prev_mask2 > prev_mask1) continue;
for(int v = 0; v < n; v++)
dp[mask][v] = min(dp[prev_mask1][v] + dp[prev_mask2][v], dp[mask][v]);
}
for(int v = 0; v < n; v++) if(dp[mask][v] < INF) pq.emplace(dp[mask][v], v);
fill(mark, mark+n, 0);
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
if(mark[u]) continue;
mark[u] = true;
for(auto p: g[u]) if(dp[mask][u] + p.second < dp[mask][p.first]) {
dp[mask][p.first] = dp[mask][u] + p.second;
pq.emplace(dp[mask][p.first], p.first);
}
}
}
ll ans = INF;
for(int v = 0; v < n; v++) ans = min(dp[(1 << k)-1][v], ans);
printf("%lld", 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... |