CF1725M.Moving Both Hands

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Pak Chanek is playing one of his favourite board games. In the game, there is a directed graph with NN vertices and MM edges. In the graph, edge ii connects two different vertices UiU_i and ViV_i with a length of WiW_i. By using the ii-th edge, something can move from UiU_i to ViV_i, but not from ViV_i to UiU_i.

To play this game, initially Pak Chanek must place both of his hands onto two different vertices. In one move, he can move one of his hands to another vertex using an edge. To move a hand from vertex UiU_i to vertex ViV_i, Pak Chanek needs a time of WiW_i seconds. Note that Pak Chanek can only move one hand at a time. This game ends when both of Pak Chanek's hands are on the same vertex.

Pak Chanek has several questions. For each pp satisfying 2≤p≤N2 \leq p \leq N, you need to find the minimum time in seconds needed for Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex 11 and vertex pp, or report if it is impossible.

帕克·查内克正在玩他最喜爱的棋盘游戏之一。在该游戏中,存在一个包含 NN 个顶点和 MM 条边的有向图。图中第 ii 条边连接两个不同的顶点 UiU_i 和 ViV_i,其长度为 WiW_i。利用第 ii 条边,某物可以从 UiU_i 移动到 ViV_i,但不能从 ViV_i 移动到 UiU_i。

为了进行该游戏,帕克·查内克初始时必须将他的两只手分别放在两个不同的顶点上。在一次操作中,他可以使用一条边将其一只手移动到另一个顶点。将一只手从顶点 UiU_i 移动到顶点 ViV_i 需要 WiW_i 秒。注意:帕克·查内克每次只能移动一只手。当他的两只手最终位于同一顶点时,游戏结束。

帕克·查内克提出了若干问题。对每个满足 2≤p≤N2 \leq p \leq N 的 pp,你需要求出:若初始时帕克·查内克的左手和右手分别位于顶点 11 和顶点 pp,则他结束游戏所需的最短时间(单位:秒);若不可能实现,则报告不可能。

输入格式

The first line contains two integers NN and MM (2≤N≤1052 \leq N \leq 10^5, 0≤M≤2⋅1050 \leq M \leq 2 \cdot 10^5) — the number of vertices and edges in the graph.

The ii-th of the next MM lines contains three integers UiU_i, ViV_i, and WiW_i (1≤Ui,Vi≤N1 \le U_i, V_i \le N, Ui≠ViU_i \neq V_i, 1≤Wi≤1091 \le W_i \le 10^9) — a directed edge that connects two different vertices UiU_i and ViV_i with a length of WiW_i. There is no pair of different edges ii and jj such that Ui=UjU_i = U_j and Vi=VjV_i = V_j.

第一行包含两个整数 NN 和 MM(2≤N≤1052 \leq N \leq 10^5,0≤M≤2⋅1050 \leq M \leq 2 \cdot 10^5)—— 分别表示图中顶点和边的数量。

接下来的 MM 行中,第 ii 行包含三个整数 UiU_i、ViV_i 和 WiW_i(1≤Ui,Vi≤N1 \le U_i, V_i \le N,Ui≠ViU_i \neq V_i,1≤Wi≤1091 \le W_i \le 10^9)—— 表示一条从顶点 UiU_i 指向顶点 ViV_i 的有向边,其长度为 WiW_i。不存在两个不同的边 ii 和 jj,使得 Ui=UjU_i = U_j 且 Vi=VjV_i = V_j。

输出格式

Output a line containing N−1N-1 integers. The jj-th integer represents the minimum time in seconds needed by Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex 11 and vertex j+1j+1, or −1-1 if it is impossible.

输出一行,包含 N−1N-1 个整数。其中第 jj 个整数表示:若游戏初始时 Pak Chanek 的左手和右手分别位于顶点 11 和顶点 j+1j+1,则 Pak Chanek 结束游戏所需的最短时间(单位:秒);若无法结束游戏,则该整数为 −1-1。

输入输出样例

  • 输入#1

    5 7
    1 2 2
    2 4 1
    4 1 4
    2 5 3
    5 4 1
    5 2 4
    2 1 1

    输出#1

    1 -1 3 4

说明/提示

If initially Pak Chanek's left hand is on vertex 11 and his right hand is on vertex 55, Pak Chanek can do the following moves:

  1. Move his right hand to vertex 44 in 11 second.
  2. Move his left hand to vertex 22 in 22 seconds.
  3. Move his left hand to vertex 44 in 11 second.

In total it needs 1+2+1=41+2+1=4 seconds. It can be proven that there is no other way that is faster.

如果最初 Pak Chanek 的左手位于顶点 11,右手位于顶点 55,则 Pak Chanek 可以执行以下操作:

  1. 将右手移动到顶点 44,耗时 11 秒。
  2. 将左手移动到顶点 22,耗时 22 秒。
  3. 将左手移动到顶点 44,耗时 11 秒。

总共需要 1+2+1=41+2+1=4 秒。可以证明不存在更快的方案。

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

首页