A15291 | Moving Both Hands
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Pak Chanek is playing one of his favourite board games. In the game, there is a directed graph with $N$ vertices and $M$ edges. In the graph, edge $i$ connects two different vertices $U_i$ and $V_i$ with a length of $W_i$ . By using the $i$ -th edge, something can move from $U_i$ to $V_i$ , but not from $V_i$ to $U_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 $U_i$ to vertex $V_i$ , Pak Chanek needs a time of $W_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 $p$ satisfying $2 \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 $1$ and vertex $p$ , or report if it is impossible.
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 $U_i$ to vertex $V_i$ , Pak Chanek needs a time of $W_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 $p$ satisfying $2 \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 $1$ and vertex $p$ , or report if it is impossible.
输入格式
The first line contains two integers $N$ and $M$ ( $2 \leq N \leq 10^5$ , $0 \leq M \leq 2 \cdot 10^5$ ) — the number of vertices and edges in the graph.
The $i$ -th of the next $M$ lines contains three integers $U_i$ , $V_i$ , and $W_i$ ( $1 \le U_i, V_i \le N$ , $U_i \neq V_i$ , $1 \le W_i \le 10^9$ ) — a directed edge that connects two different vertices $U_i$ and $V_i$ with a length of $W_i$ . There is no pair of different edges $i$ and $j$ such that $U_i = U_j$ and $V_i = V_j$ .
The $i$ -th of the next $M$ lines contains three integers $U_i$ , $V_i$ , and $W_i$ ( $1 \le U_i, V_i \le N$ , $U_i \neq V_i$ , $1 \le W_i \le 10^9$ ) — a directed edge that connects two different vertices $U_i$ and $V_i$ with a length of $W_i$ . There is no pair of different edges $i$ and $j$ such that $U_i = U_j$ and $V_i = V_j$ .
输出格式
Output a line containing $N-1$ integers. The $j$ -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 $1$ and vertex $j+1$ , or $-1$ if it is impossible.
输入输出样例
输入 #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 $1$ and his right hand is on vertex $5$ , Pak Chanek can do the following moves:
1. Move his right hand to vertex $4$ in $1$ second.
2. Move his left hand to vertex $2$ in $2$ seconds.
3. Move his left hand to vertex $4$ in $1$ second.

In total it needs $1+2+1=4$ seconds. It can be proven that there is no other way that is faster.
1. Move his right hand to vertex $4$ in $1$ second.
2. Move his left hand to vertex $2$ in $2$ seconds.
3. Move his left hand to vertex $4$ in $1$ second.

In total it needs $1+2+1=4$ seconds. It can be proven that there is no other way that is faster.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted