A14132 | Paired Payment
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There are $n$ cities and $m$ 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 $w$ . 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 $a$ to city $b$ and then from city $b$ to city $c$ ) and you will have to pay $(w_{ab} + w_{bc})^2$ money to go through those roads. Find out whether it is possible to travel from city $1$ to every other city $t$ and what's the minimum amount of money you need to get from $1$ to $t$ .
输入格式
First line contains two integers $n$ , $m$ ( $2 \leq n \leq 10^5$ , $1 \leq m \leq min(\frac{n \cdot (n - 1)}{2}, 2 \cdot 10^5)$ ).
Next $m$ lines each contain three integers $v_i$ , $u_i$ , $w_i$ ( $1 \leq v_i, u_i \leq n$ , $1 \leq w_i \leq 50$ , $u_i \neq v_i$ ). It's guaranteed that there are no multiple edges, i.e. for any edge $(u_i, v_i)$ there are no other edges $(u_i, v_i)$ or $(v_i, u_i)$ .
Next $m$ lines each contain three integers $v_i$ , $u_i$ , $w_i$ ( $1 \leq v_i, u_i \leq n$ , $1 \leq w_i \leq 50$ , $u_i \neq v_i$ ). It's guaranteed that there are no multiple edges, i.e. for any edge $(u_i, v_i)$ there are no other edges $(u_i, v_i)$ or $(v_i, u_i)$ .
输出格式
For every city $t$ print one integer. If there is no correct path between $1$ and $t$ output $-1$ . Otherwise print out the minimum amount of money needed to travel from $1$ to $t$ .
输入输出样例
输入 #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 $1$ to $3$ goes through $2$ , so the resulting payment is $(1 + 2)^2 = 9$ .


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

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