A12342 | Make It Connected
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an undirected graph consisting of $n$ vertices. A number is written on each vertex; the number on vertex $i$ is $a_i$ . 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 $x$ and $y$ is $a_x + a_y$ coins. There are also $m$ special offers, each of them is denoted by three numbers $x$ , $y$ and $w$ , and means that you can add an edge connecting vertices $x$ and $y$ and pay $w$ coins for it. You don't have to use special offers: if there is a pair of vertices $x$ and $y$ that has a special offer associated with it, you still may connect these two vertices paying $a_x + a_y$ 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.
You may add some edges to this graph, but you have to pay for them. The cost of adding an edge between vertices $x$ and $y$ is $a_x + a_y$ coins. There are also $m$ special offers, each of them is denoted by three numbers $x$ , $y$ and $w$ , and means that you can add an edge connecting vertices $x$ and $y$ and pay $w$ coins for it. You don't have to use special offers: if there is a pair of vertices $x$ and $y$ that has a special offer associated with it, you still may connect these two vertices paying $a_x + a_y$ 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.
输入格式
The first line contains two integers $n$ and $m$ ( $1 \le n \le 2 \cdot 10^5$ , $0 \le m \le 2 \cdot 10^5$ ) — the number of vertices in the graph and the number of special offers, respectively.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^{12}$ ) — the numbers written on the vertices.
Then $m$ lines follow, each containing three integers $x$ , $y$ and $w$ ( $1 \le x, y \le n$ , $1 \le w \le 10^{12}$ , $x \ne y$ ) denoting a special offer: you may add an edge connecting vertex $x$ and vertex $y$ , and this edge will cost $w$ coins.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^{12}$ ) — the numbers written on the vertices.
Then $m$ lines follow, each containing three integers $x$ , $y$ and $w$ ( $1 \le x, y \le n$ , $1 \le w \le 10^{12}$ , $x \ne y$ ) denoting a special offer: you may add an edge connecting vertex $x$ and vertex $y$ , and this edge will cost $w$ coins.
输出格式
Print one integer — the minimum number of coins you have to pay to make the graph connected.
输入输出样例
输入 #1
3 2 1 3 3 2 3 5 2 1 1
输出 #1
5
输入 #2
4 0 1 3 3 7
输出 #2
16
输入 #3
5 4 1 2 3 4 5 1 2 8 1 3 10 1 4 7 1 5 15
输出 #3
18
In the first example it is possible to connect $1$ to $2$ using special offer $2$ , and then $1$ to $3$ without using any offers.
In next two examples the optimal answer may be achieved without using special offers.
In next two examples the optimal answer may be achieved without using special offers.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted