# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
202260 |
2020-02-14T15:45:00 Z |
DS007 |
Cities (BOI16_cities) |
C++14 |
|
1956 ms |
26776 KB |
#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
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 |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Incorrect |
8 ms |
5144 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1265 ms |
26696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
5240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1564 ms |
26764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1956 ms |
26776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |