CF1486E.Paired Payment

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

There are nn cities and mm bidirectional roads in the country. The roads in the country form an undirected weighted graph. The graph is not guaranteed to be connected. Each road has it's own parameter ww. You can travel through the roads, but the government made a new law: you can only go through two roads at a time (go from city aa to city bb and then from city bb to city cc) and you will have to pay (wab+wbc)2(w_{ab} + w_{bc})^2 money to go through those roads. Find out whether it is possible to travel from city 11 to every other city tt and what's the minimum amount of money you need to get from 11 to tt.

该国共有 nn 座城市和 mm 条双向道路。这些道路构成一个无向带权图。该图不一定连通。每条道路都有其自身的权重参数 ww。你可以沿道路通行,但政府颁布了一项新法规:每次出行仅允许连续经过两条道路(即从城市 aa 到城市 bb,再从城市 bb 到城市 cc),且通行这两条道路需支付 (wab+wbc)2(w_{ab} + w_{bc})^2 的费用。请判断是否可以从城市 11 到达其余每个城市 tt,并求出从城市 11 到达城市 tt 所需的最少费用。

输入格式

First line contains two integers nn, mm (2≤n≤1052 \leq n \leq 10^5, 1≤m≤min(n⋅(n−1)2,2⋅105)1 \leq m \leq min(\frac{n \cdot (n - 1)}{2}, 2 \cdot 10^5)).

Next mm lines each contain three integers viv_i, uiu_i, wiw_i (1≤vi,ui≤n1 \leq v_i, u_i \leq n, 1≤wi≤501 \leq w_i \leq 50, ui≠viu_i \neq v_i). It's guaranteed that there are no multiple edges, i.e. for any edge (ui,vi)(u_i, v_i) there are no other edges (ui,vi)(u_i, v_i) or (vi,ui)(v_i, u_i).

第一行包含两个整数 nn 和 mm(2≤n≤1052 \leq n \leq 10^5,1≤m≤min⁡(n⋅(n−1)2,2⋅105)1 \leq m \leq \min\left(\frac{n \cdot (n - 1)}{2}, 2 \cdot 10^5\right))。

接下来的 mm 行,每行包含三个整数 viv_i、uiu_i、wiw_i(1≤vi,ui≤n1 \leq v_i, u_i \leq n,1≤wi≤501 \leq w_i \leq 50,ui≠viu_i \neq v_i)。保证图中不存在重边,即对任意边 (ui,vi)(u_i, v_i),不存在其他边 (ui,vi)(u_i, v_i) 或 (vi,ui)(v_i, u_i)。

输出格式

For every city tt print one integer. If there is no correct path between 11 and tt output −1-1. Otherwise print out the minimum amount of money needed to travel from 11 to tt.

对于每个城市 tt,输出一个整数。如果城市 11 与城市 tt 之间不存在合法路径,则输出 −1-1;否则输出从城市 11 到城市 tt 所需的最少金额。

输入输出样例

  • 输入#1

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

    输出#1

    0 98 49 25 114
  • 输入#2

    3 2
    1 2 1
    2 3 2

    输出#2

    0 -1 9

说明/提示

The graph in the first example looks like this.

In the second example the path from 11 to 33 goes through 22, so the resulting payment is (1+2)2=9(1 + 2)^2 = 9.

第一个样例中的图如下所示。

在第二个样例中,从节点 11 到节点 33 的路径经过节点 22,因此最终支付金额为 (1+2)2=9(1 + 2)^2 = 9。

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

首页