CF294E.Shaass the Great

提高+/省选-

通过率:0%

时间限制:3.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

The great Shaass is the new king of the Drakht empire. The empire has n cities which are connected by n - 1 bidirectional roads. Each road has an specific length and connects a pair of cities. There's a unique simple path connecting each pair of cities.

His majesty the great Shaass has decided to tear down one of the roads and build another road with the same length between some pair of cities. He should build such road that it's still possible to travel from each city to any other city. He might build the same road again.

You as his advisor should help him to find a way to make the described action. You should find the way that minimize the total sum of pairwise distances between cities after the action. So calculate the minimum sum.

伟大的沙阿斯(Shaass)成为了德拉赫特(Drakht)帝国的新国王。该帝国共有 nn 座城市,由 n−1n-1 条双向道路连接。每条道路具有特定的长度,并连接一对城市。任意两座城市之间均存在唯一的一条简单路径。

伟大的沙阿斯陛下决定拆除其中一条道路,并在某对城市之间新建一条长度相同的道路。新建道路后,仍需保证任意两座城市之间均可互相到达(即图保持连通)。他甚至可能重建被拆除的同一条道路。

作为他的顾问,你需要协助他完成上述操作。你需要找到一种方案,使得操作完成后所有城市对之间的距离总和最小。请计算该最小总和。

输入格式

The first line of the input contains an integer n denoting the number of cities in the empire, (2 ≤ n ≤ 5000). The next n - 1 lines each contains three integers a__i, b__i and w__i showing that two cities a__i and b__i are connected using a road of length w__i, (1 ≤ a__i, b__i ≤ n, a__i ≠ b__i, 1 ≤ w__i ≤ 106).

输入的第一行包含一个整数 nn,表示帝国中城市的数量(2 ≤ n ≤ 50002 \leq n \leq 5000)。接下来的 n − 1n - 1 行每行包含三个整数 aia_i、bib_i 和 wiw_i,表示城市 aia_i 与城市 bib_i 之间通过一条长度为 wiw_i 的道路相连(1 ≤ ai, bi ≤ n1 \leq a_i, b_i \leq n,ai ≠ bia_i \neq b_i,1 ≤ wi ≤ 1061 \leq w_i \leq 10^6)。

输出格式

On the only line of the output print the minimum pairwise sum of distances between the cities.

Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.

在输出的唯一一行中,打印城市之间两两距离的最小和。

请勿在 C++ 中使用 %lld 格式说明符读取或写入 64 位整数。推荐使用 cin、cout 流,或 %I64d 格式说明符。

输入输出样例

  • 输入#1

    3
    1 2 2
    1 3 4

    输出#1

    12
  • 输入#2

    6
    1 2 1
    2 3 1
    3 4 1
    4 5 1
    5 6 1

    输出#2

    29
  • 输入#3

    6
    1 3 1
    2 3 1
    3 4 100
    4 5 2
    4 6 1

    输出#3

    825

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

首页