Make It Connected
CodeForces - 1095F
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
3 2
1 3 3
2 3 5
2 1 1
Output
5
Input
4 0
1 3 3 7
Output
16
Input
5 4
1 2 3 4 5
1 2 8
1 3 10
1 4 7
1 5 15
Output
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个点,每个点有一个点权,第i个点的点权为a_i
此时,他的脑海里竟然浮现出了一段文字:
卑鄙的异乡人啊,
你太年轻太简单了,有时还很朴素
我需要给你一些微小的考验
所有点联通之时,
返程之路将浮现。
赵老师知道,想要在点i和点j之间连一条边,所需时间为a_i+a_j
但是作为一个单身多年的魔法师,他可以施展m次魔法,第i次魔法可以在x_i和y_i之间连一条边,所需时间是w_i
赵老师清楚的记得,第二天他还需要上课,因此你需要帮他算出将所有点联通所需的最短时间是多少
思路:
将每一个节点和除了它自己以外的其他n-1个节点中,权值最小的节点相连接。
这样一共是n个边,
还有题目给出的m个边。
在这n+m个边中跑Kruskal算法,求出最小生成树的代价即可。
代码:
#include
#include
#include
#include
#include
#include
#include
#include