#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>
using namespace std;
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define int long long
struct UFDS {
int n;
vector<int> info;
UFDS() {}
UFDS(int _n) { init(_n); }
void init(int _n) {
n = _n;
info.assign(n, -1);
}
int get(int x) {
if (info[x] < 0) return x;
return info[x] = get(info[x]);
}
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return false;
if (info[x] > info[y])
swap(x, y);
info[x] += info[y];
info[y] = x;
return true;
}
bool connected(int x, int y) { return get(x) == get(y); }
};
struct Edge {
int u, v, w;
Edge(int u, int v, int w) : u(u), v(v), w(w) {}
bool operator<(const Edge &other) const {
return w < other.w;
}
};
const int MXN = 1e5 + 5, INF = 1e18;
void solve() {
int N, K, M;
cin >> N >> K >> M;
vector<int> dist(N, INF), par(N, -1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
for (int i = 0; i < K; i++) {
int v;
cin >> v;
v--;
dist[v] = 0;
par[v] = v;
pq.emplace(0, v);
}
vector<Edge> edges;
vector<pair<int, int>> g[N];
for (int i = 0; i < M; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
edges.emplace_back(u, v, w);
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
while (!pq.empty()) {
auto [cur_dist, u] = pq.top();
pq.pop();
if (cur_dist != dist[u]) continue;
for (const auto &[v, w] : g[u]) {
int new_dist = cur_dist + w;
if (new_dist < dist[v]) {
dist[v] = new_dist;
par[v] = par[u];
pq.emplace(new_dist, v);
}
}
}
vector<Edge> kruskals;
for (const auto &[u, v, w] : edges) {
if (par[u] != par[v]) {
kruskals.emplace_back(par[u], u, dist[u]);
kruskals.emplace_back(par[v], v, dist[v]);
kruskals.emplace_back(u, v, w);
}
}
sort(all(kruskals));
UFDS ufds(N);
int cost = 0;
for (const auto &[u, v, w] : kruskals) {
if (ufds.unite(u, v))
cost += w;
}
cout << cost;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int TC = 1;
// cin >> TC;
while (TC--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
218 ms |
34568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
227 ms |
35184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
225 ms |
35436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |