# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
37263 | szawinis | Cities (BOI16_cities) | C++14 | 516 ms | 16116 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];
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 c = 0; c < n; c++) {
for(int i1 = 0; i1 < k; i1++) for(int i2 = 0; i2 < k; i2++) {
if(i1 != i2 && d[i1][c] + d[i2][c] == d[i1][s[i2]]) {
ans = min(d[0][c] + d[1][c] + (k == 3 ? d[2][c] : 0), ans);
break;
}
}
}
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... |