#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld long double
#define all(a) (a).begin(), (a).end()
#define unique(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define vi(name, n, a) vector<ll> name(n, a)
#define vll(name, n, a) vector<ll> name(n, a)
#define vlld(name, n, a) vector<ll> name(n, a)
#define vp(name, n, a) vector<pair<ll,ll>> name(n, {a, a});
#define vi2(name, n, m, value) vector<vector<ll>> name(n, vector<ll>(m, value))
#define vll2(name, n, m, value) vector<vector<ll>> name(n, vector<ll>(m, value))
#define vlld2(name, n, m, value) vector<vector<ll>> name(n, vector<ll>(m, value))
#define pb(x) push_back(x)
const ll mod = 1e9+7;
const ll N = 500009;
ll res = 0, k;
vector<pair<ll,ll>> graph[N];
//vector<pair<ll, pair<ll,ll>>> edges;
set<pair<ll,pair<ll,ll>>> edges;
vector<ll> a(N);
ll dp[N];
vector<bool> isVisited(N, false);
vector<bool> dfsVisited(N, false);
ll dist[N];
ll x[N];
ll sz[N];
ll parent[N];
vector<pair<ll,pair<ll,ll>>> ans;
ll cnt[N], special[N];
ll M;
void make(){
for(ll i=0; i<N; i++){
sz[i] = 1;
parent[i] = i;
}
}
ll find(ll a){
if(a == parent[a]) return a;
return parent[a] = find(parent[a]);
}
void Union(ll a, ll b){
a = find(a);
b = find(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a, b);
parent[b] = a;
sz[a] += sz[b];
}
void MST(){
while(edges.size()>0){
pair<ll,pair<ll,ll>> p = *edges.begin();
edges.erase(edges.begin());
ll w = p.first;
ll u = p.second.first;
ll v = p.second.second;
ll cost = a[u] + a[v];
if(find(u) != find(v)){
if(v != M){
//ans.push_back({cost, {u, v}});
res += cost;
// edges.insert({cost, {u, v}});
Union(u, v);
}
else if(edges.size() > 0){
pair<ll,pair<ll,ll>> p2 = *edges.begin();
edges.erase(edges.begin());
ll w2 = p.first;
ll u2 = p.second.first;
ll v2 = p.second.second;
if(v2 == M){
ll cost = a[u] + a[u2];
if(find(u) != find(u2)){
//ans.push_back({cost, {v, v2}});
res += cost;
//edges.insert({cost, {v, v2}});
Union(u, u2);
edges.insert(p);
edges.insert(p2);
}
}
}
}
}
}
void solve(){
ll n,m;
cin >> n >> m;
for(ll i=1; i<=n; i++){
cin >> a[i];
}
ans.clear();edges.clear();
for(ll i=0; i<m; i++){
ll u,v,w;
cin >> u >> v >> w;
//u--;v--;
edges.insert({w, {u,v}});
}
M = n+1;
a[M] = 0;
for(ll i=1; i<=n; i++){
edges.insert({a[M] + a[i], {i, n+1}});
}
//sort(all(edges));
MST();
cout << res << endl;
}
int main(){
ll t = 1;
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
memset(special, 0, sizeof(special));
make();
for(ll i=0; i<N; i++) graph[i].clear();
while (t--) solve();
return 0;
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}