CF1023F.Mobile Phone Network

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are managing a mobile phone network, and want to offer competitive prices to connect a network.

The network has nn nodes.

Your competitor has already offered some connections between some nodes, with some fixed prices. These connections are bidirectional. There are initially mm connections the competitor is offering. The ii-th connection your competitor is offering will connect nodes faifa_i and fbifb_i and costs fwifw_i.

You have a list of kk connections that you want to offer. It is guaranteed that this set of connection does not form any cycle. The jj-th of these connections will connect nodes gajga_j and gbjgb_j. These connections are also bidirectional. The cost of these connections have not been decided yet.

You can set the prices of these connections to any arbitrary integer value. These prices are set independently for each connection. After setting the prices, the customer will choose such n−1n - 1 connections that all nodes are connected in a single network and the total cost of chosen connections is minimum possible. If there are multiple ways to choose such networks, the customer will choose an arbitrary one that also maximizes the number of your connections in it.

You want to set prices in such a way such that all your kk connections are chosen by the customer, and the sum of prices of your connections is maximized.

Print the maximum profit you can achieve, or −1-1 if it is unbounded.

你正在运营一个手机网络,希望提供有竞争力的价格来连接整个网络。

该网络包含 nn 个节点。

你的竞争对手已预先提供了一些节点之间的连接,并设定了固定价格。这些连接是双向的。初始时,竞争对手共提供了 mm 条连接。其中第 ii 条连接连接节点 faifa_i 和 fbifb_i,费用为 fwifw_i。

你手头还有一份共 kk 条你打算提供的连接列表。保证这些连接本身不构成任何环。其中第 jj 条连接连接节点 gajga_j 和 gbjgb_j,同样是双向的。这些连接的费用尚未确定。

你可以将每条连接的费用设定为任意整数(各连接独立设定)。设定完所有费用后,客户将从中选择恰好 n−1n - 1 条连接,使得全部 nn 个节点连通成一棵树,且所选连接的总费用最小。若存在多种总费用相同的方案,客户将在所有总费用最小的方案中,任意选择一个包含你提供的连接数量最多的方案。

你希望设定费用,使得客户最终所选的树恰好包含你全部 kk 条连接,并且你这 kk 条连接的总费用之和尽可能大。

请输出你能获得的最大总利润;若该最大值无上界,则输出 −1-1。

输入格式

The first line of input will contain three integers nn, kk and mm (1≤n,k,m≤5⋅105,k≤n−11 \leq n, k, m \leq 5 \cdot 10^5, k \leq n-1), the number of nodes, the number of your connections, and the number of competitor connections, respectively.

The next kk lines contain two integers gaiga_i and gbigb_i (1≤gai,gbi≤n1 \leq ga_i, gb_i \leq n, gai≠gbiga_i \not= gb_i), representing one of your connections between nodes gaiga_i and gbigb_i. Your set of connections is guaranteed to be acyclic.

The next mm lines contain three integers each, faifa_i, fbifb_i and fwifw_i (1≤fai,fbi≤n1 \leq fa_i, fb_i \leq n, fai≠fbifa_i \not= fb_i, 1≤fwi≤1091 \leq fw_i \leq 10^9), denoting one of your competitor's connections between nodes faifa_i and fbifb_i with cost fwifw_i. None of these connections connects a node to itself, and no pair of these connections connect the same pair of nodes. In addition, these connections are given by non-decreasing order of cost (that is, fwi−1≤fwifw_{i-1} \leq fw_i for all valid ii).

Note that there may be some connections that appear in both your set and your competitor's set (though no connection will appear twice in one of this sets).

It is guaranteed that the union of all of your connections and your competitor's connections form a connected network.

输入的第一行包含三个整数 nn、kk 和 mm(1≤n,k,m≤5⋅1051 \leq n, k, m \leq 5 \cdot 10^5,且 k≤n−1k \leq n-1),分别表示节点数量、你的连接数量以及竞争对手的连接数量。

接下来的 kk 行每行包含两个整数 gaiga_i 和 gbigb_i(1≤gai,gbi≤n1 \leq ga_i, gb_i \leq n,且 gai≠gbiga_i \neq gb_i),表示你的一条连接,其两端节点为 gaiga_i 和 gbigb_i。保证你的所有连接构成一个无环图(即森林)。

接下来的 mm 行每行包含三个整数 faifa_i、fbifb_i 和 fwifw_i(1≤fai,fbi≤n1 \leq fa_i, fb_i \leq n,fai≠fbifa_i \neq fb_i,且 1≤fwi≤1091 \leq fw_i \leq 10^9),表示竞争对手的一条连接,其两端节点为 faifa_i 和 fbifb_i,代价为 fwifw_i。这些连接均不连接节点到自身,且任意两条连接不会连接完全相同的节点对。此外,这些连接按代价非递减顺序给出(即对所有合法的 ii,均有 fwi−1≤fwifw_{i-1} \leq fw_i)。

注意:可能存在某些连接同时出现在你的连接集合与竞争对手的连接集合中(但任一集合内部不会出现重复连接)。

保证你与竞争对手的所有连接的并集构成一个连通网络。

输出格式

Print a single integer, the maximum possible profit you can achieve if you set the prices on your connections appropriately. If the profit is unbounded, print −1-1.

输出一个整数,表示若你恰当地设定各连接的价格,所能获得的最大可能利润。如果利润无上界,则输出 −1-1。

输入输出样例

  • 输入#1

    4 3 6
    1 2
    3 4
    1 3
    2 3 3
    3 1 4
    1 2 4
    4 2 8
    4 3 8
    4 1 10

    输出#1

    14
  • 输入#2

    3 2 1
    1 2
    2 3
    1 2 30

    输出#2

    -1
  • 输入#3

    4 3 3
    1 2
    1 3
    1 4
    4 1 1000000000
    4 2 1000000000
    4 3 1000000000

    输出#3

    3000000000

说明/提示

In the first sample, it's optimal to give connection 1−31-3 cost 33, connection 1−21-2 cost 33, and connection 3−43-4 cost 88. In this case, the cheapest connected network has cost 1414, and the customer will choose one that chooses all of your connections.

In the second sample, as long as your first connection costs 3030 or less, the customer chooses both your connections no matter what is the cost of the second connection, so you can get unbounded profit in this case.

在第一个样例中,最优策略是将连接 1−31-3 的费用设为 33,连接 1−21-2 的费用设为 33,连接 3−43-4 的费用设为 88。此时,最便宜的连通网络总费用为 1414,客户将选择包含你所有连接的方案。

在第二个样例中,只要你的第一条连接费用不超过 3030,无论第二条连接的费用是多少,客户都会选择你的两条连接,因此在此情况下你可以获得无界的利润。

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

首页