Codeforces 1095 F. Make It Connected (MST)

F. Make It Connected

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an undirected graph consisting of nn vertices. A number is written on each vertex; the number on vertex ii is aiai. Initially there are no edges in the graph.

You may add some edges to this graph, but you have to pay for them. The cost of adding an edge between vertices xx and yy is ax+ayax+ay coins. There are also mm special offers, each of them is denoted by three numbers xx, yy and ww, and means that you can add an edge connecting vertices xx and yy and pay ww coins for it. You don't have to use special offers: if there is a pair of vertices xx and yy that has a special offer associated with it, you still may connect these two vertices paying ax+ayax+ay coins for it.

What is the minimum number of coins you have to spend to make the graph connected? Recall that a graph is connected if it's possible to get from any vertex to any other vertex using only the edges belonging to this graph.

Input

The first line contains two integers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 0≤m≤2⋅1050≤m≤2⋅105) — the number of vertices in the graph and the number of special offers, respectively.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10121≤ai≤1012) — the numbers written on the vertices.

Then mm lines follow, each containing three integers xx, yy and ww (1≤x,y≤n1≤x,y≤n, 1≤w≤10121≤w≤1012, x≠yx≠y) denoting a special offer: you may add an edge connecting vertex xx and vertex yy, and this edge will cost ww coins.

Output

Print one integer — the minimum number of coins you have to pay to make the graph connected.

Examples

input

Copy

3 2
1 3 3
2 3 5
2 1 1

output

Copy

5

input

Copy

4 0
1 3 3 7

output

Copy

16

input

Copy

5 4
1 2 3 4 5
1 2 8
1 3 10
1 4 7
1 5 15

output

Copy

18

Note

In the first example it is possible to connect 11 to 22 using special offer 22, and then 11 to 33 without using any offers.

In next two examples the optimal answer may be achieved without using special offers.

题目大意:

    给你N个点,每个点有一个权值a,已知连接两点i与j的代价为ai + aj​,现在还有其他的M种连接方法,连接u,v的费用为w. 求出让这个图连通的最小代价。

解法:

    对于按照点权之和的连接方案,容易想到找到最小的点权,其他所有点都连他这一个点就好了,那么就在M条边的基础上添加N-1条边,再求一次MST。

Accepted code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

struct node
{
	ll w;
	int u, v;
	bool operator < (const node &oth) const {
		return w < oth.w;
	}
}a[N << 1];
int fz[N], n, m, o, cnt;
ll val[N], mi = LINF;

int Find(int x) {
	if (x != fz[x])
		x = fz[x] = Find(fz[x]);
	return x;
}

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		fz[i] = i;
		sc("%lld", &val[i]);
		if (val[i] < mi)
			mi = val[i], o = i;   // 找到最小点权的点
	}
	for (int i = 1; i <= m; i++)
		sc("%d %d %lld", &a[i].u, &a[i].v, &a[i].w);
	cnt = m;
	for (int i = 1; i <= n; i++) {
		if (i != o)
			a[++cnt] = { val[o] + val[i], o, i };  // 加新边
	}
	sort(a + 1, a + cnt + 1);

	ll ans = 0;
	for (int i = 1; i <= cnt; i++) {   // MST
		int u = Find(a[i].u), v = Find(a[i].v);
		if (u != v)
			fz[u] = v, ans += a[i].w;
	}
	printf("%lld\n", ans);
	return 0; // 改数组大小!!!用pair改宏定义!!!
}

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/110224958