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;
const int N = 1e5;
int n, m, k;
vector<pair<int, long long>> adj[N];
bool is_k[N];
vector<int> k_;
vector<pair<int, long long>> res[N];
auto cmp = [](pair<int, long long> &a, pair<int, long long> &b) {
return a.second > b.second;
};
void dijkstra(int v) {
long long dist[n];
fill(dist, dist + n, 1e15);
dist[v] = 0;
priority_queue<pair<int, long long>, vector<pair<int, long long>>, decltype(cmp)> pq(cmp);
pq.push({v, 0});
set<int> settled;
while (settled.size() != n) {
auto t = pq.top();
pq.pop();
if (settled.count(t.first))
continue;
settled.insert(t.first);
for (auto i : adj[t.first]) {
dist[i.first] = min(dist[i.first], dist[t.first] + i.second);
pq.push({i.first, dist[i.first]});
}
}
for (int i = 0; i < n; i++) {
if (i != v && is_k[i])
res[v].emplace_back(i, dist[i]);
}
}
long long score() {
priority_queue<pair<int, long long>, vector<pair<int, long long>>, decltype(cmp)> pq(cmp);
pq.push({k_[0], 0});
long long ans = 0;
set<int> settled;
while (settled.size() != k) {
auto t = pq.top();
pq.pop();
if (settled.count(t.first))
continue;
settled.insert(t.first);
ans += t.second;
for (auto i : res[t.first])
pq.push(i);
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> k >> m;
for (int i = 0; i < k; i++) {
int v; cin >> v;
k_.push_back(v - 1);
is_k[v - 1] = true;
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
for (int i = 0; i < n; i++) {
if (is_k[i])
dijkstra(i);
}
cout << score();
}
Compilation message (stderr)
cities.cpp: In function 'void dijkstra(int)':
cities.cpp:24:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (settled.size() != n) {
~~~~~~~~~~~~~~~^~~~
cities.cpp: In function 'long long int score()':
cities.cpp:51:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (settled.size() != k) {
~~~~~~~~~~~~~~~^~~~
# | 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... |