CF1095F.Make It Connected

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an undirected graph consisting of nn vertices. A number is written on each vertex; the number on vertex ii is aia_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 xx and yy is ax+aya_x + a_y 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+aya_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.

给你一个包含 nn 个顶点的无向图。每个顶点上写有一个数字;顶点 ii 上的数字为 aia_i。初始时图中没有边。

你可以向该图中添加若干条边,但需要为此付费。在顶点 xx 和 yy 之间添加一条边的费用为 ax+aya_x + a_y 枚硬币。此外还有 mm 个特殊优惠,每个优惠用三个数 xx、yy 和 ww 表示,含义是:你可以在顶点 xx 和 yy 之间添加一条边,并仅需支付 ww 枚硬币。你并非必须使用这些特殊优惠:即使顶点对 xx 和 yy 存在对应的特殊优惠,你仍可以选择按常规方式连接它们,即支付 ax+aya_x + a_y 枚硬币。

为使该图连通,你至少需要花费多少枚硬币?注意,一个图被称为连通图,当且仅当图中任意两个顶点之间均存在仅由该图中的边构成的路径。

输入格式

The first line contains two integers nn and mm (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, 0≤m≤2⋅1050 \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 nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤10121 \le a_i \le 10^{12}) — the numbers written on the vertices.

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

第一行包含两个整数 nn 和 mm(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5,0≤m≤2⋅1050 \le m \le 2 \cdot 10^5),分别表示图中顶点的数量和特殊优惠的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤10121 \le a_i \le 10^{12}),表示写在各顶点上的数字。

接下来是 mm 行,每行包含三个整数 xx、yy 和 ww(1≤x,y≤n1 \le x, y \le n,1≤w≤10121 \le w \le 10^{12},x≠yx \ne y),表示一项特殊优惠:你可以添加一条连接顶点 xx 和顶点 yy 的边,该边的花费为 ww 枚金币。

输出格式

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 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.

在第一个例子中,可以使用特价优惠 2 将节点 11 与节点 22 连接,然后不使用任何优惠即可将节点 11 与节点 33 连接。

在接下来的两个例子中,最优解可在不使用任何特价优惠的情况下达成。

输入解题思路,AI测评打分。不知道怎么写?

首页