Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define int long long
- using namespace std;
- void Read(int &val) {
- val = 0; char c;
- do { c = getchar(); } while (!isdigit(c));
- while (isdigit(c)) { val = val * 10 + c - '0'; c = getchar(); }
- }
- typedef pair<int, int> ii;
- const int N = 2e5 + 4, oo = 1e16 + 4;
- int n, orgN, m, cost[N];
- ii comp[N];
- vector<int> adj[N];
- map<ii, int> Map;
- int d[N];
- priority_queue<ii, vector<ii>, greater<ii> > pq;
- void sol() {
- for (int i = 1; i <= n; ++i) d[i] = oo;
- for (int i = 1; i <= orgN; ++i) {
- d[i] = cost[i]; pq.push(ii(d[i], i));
- }
- while (pq.size()) {
- int u = pq.top().second, du = pq.top().first; pq.pop();
- if (du != d[u]) continue;
- for (int v : adj[u]) {
- if (v <= orgN) {
- assert(u > orgN);
- if (d[v] > d[u]) { d[v] = d[u]; pq.push(ii(d[v], v)); }
- }
- else {
- assert(u <= orgN);
- if (d[v] > d[comp[v].first] + d[comp[v].second]) {
- d[v] = d[comp[v].first] + d[comp[v].second];
- pq.push(ii(d[v], v));
- }
- }
- }
- }
- cout << d[1] << '\n';
- }
- #undef int
- int main() {
- #define int long long
- if (fopen("input.txt", "r")) {
- freopen("input.txt", "r", stdin);
- }
- else {
- freopen("dwarf.inp", "r", stdin);
- freopen("dwarf.out", "w", stdout);
- }
- Read(n); Read(m); orgN = n;
- for (int i = 1; i <= n; ++i) Read(cost[i]);
- for (int i = 1; i <= n; ++i) comp[i] = ii(i, i);
- int dad, u, v;
- for (int i = 1; i <= m; ++i) {
- Read(dad); Read(u); Read(v);
- if (u > v) swap(u, v);
- if (!Map[ii(u, v)]) Map[ii(u, v)] = ++n, comp[n] = ii(u, v);
- int ver = Map[ii(u, v)];
- adj[ver].push_back(dad);
- adj[u].push_back(ver); adj[v].push_back(ver);
- }
- sol();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement