#abc208d. D - Shortest Path Queries 2

D - Shortest Path Queries 2

Score : 400400 points

问题描述

在高桥王国中,有 NN 座城市和 MM 条道路。

城市编号从 11NN,道路编号从 11MM。第 ii 条道路是一条单向道路,从城市 AiA_i 指向城市 BiB_i,通过这条道路需要花费 CiC_i 分钟。

令我们定义函数 f(s,t,k)f(s, t, k) 为以下查询的答案:

  • 计算从城市 ss 到城市 tt 所需的最短时间。在此条件下,除了城市 sstt 外,只允许经过编号为 11kk 的城市。若无法到达城市 tt 或者 s=ts = t,答案应为 00

计算所有三元组 (s,t,k)(s,t,k) 对应的 f(s,t,k)f(s, t, k) 值,并打印它们的总和。更正式地,输出 $\displaystyle \sum_{s = 1}^N \sum_{t = 1}^N \sum_{k = 1}^N f(s, t, k)$.

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN cities and MM roads in the Kingdom of Takahashi.

The cities are numbered 11 through NN, and the roads are numbered 11 through MM. Road ii is a one-way road that leads from City AiA_i to City BiB_i, and it takes CiC_i minutes to go through it.

Let us define f(s,t,k)f(s, t, k) as the answer to the following query.

  • Compute the minimum time needed to get from City ss to City tt. Here, other than the Cities ss and tt, it is only allowed to pass through Cities 11 through kk. If City tt is unreachable or s=ts = t, the answer should be 00.

Compute f(s,t,k)f(s,t,k) for all triples s,t,ks,t,k and print the sum of them. More formally, print $\displaystyle \sum_{s = 1}^N \sum_{t = 1}^N \sum_{k = 1}^N f(s, t, k)$.

Constraints

  • 1N4001 \leq N \leq 400
  • 0MN(N1)0 \leq M \leq N(N-1)
  • 1AiN1 \leq A_i \leq N (1iM)(1 \leq i \leq M)
  • 1BiN1 \leq B_i \leq N (1iM)(1 \leq i \leq M)
  • AiBiA_i \neq B_i (1iM)(1 \leq i \leq M)
  • 1Ci1061 \leq C_i \leq 10^6 (1iM)(1 \leq i \leq M)
  • AiAjA_i \neq A_j or BiBjB_i \neq B_j, if iji \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 B1B_1 C1C_1

\vdots

AMA_M BMB_M CMC_M

Output

Print $\displaystyle \sum_{s = 1}^N \sum_{t = 1}^N \sum_{k = 1}^N f(s, t, k)$.

Sample Input 1

3 2
1 2 3
2 3 2

Sample Output 1

25

The triples s,t,ks,t,k such that f(s,t,k)0f(s,t,k) \neq 0 are as follows.

  • For k=1k = 1: f(1,2,1)=3,f(2,3,1)=2f(1,2,1) = 3, f(2,3,1) = 2.
  • For k=2k = 2: f(1,2,2)=3,f(2,3,2)=2,f(1,3,2)=5f(1,2,2) = 3, f(2,3,2) = 2, f(1,3,2) = 5.
  • For k=3k = 3: f(1,2,3)=3,f(2,3,3)=2,f(1,3,3)=5f(1,2,3) = 3, f(2,3,3) = 2, f(1,3,3) = 5.

Sample Input 2

3 0

Sample Output 2

0

We have f(s,t,k)=0f(s,t,k) = 0 for all s,t,ks,t,k.

Sample Input 3

5 20
1 2 6
1 3 10
1 4 4
1 5 1
2 1 5
2 3 9
2 4 8
2 5 6
3 1 5
3 2 1
3 4 7
3 5 9
4 1 4
4 2 6
4 3 4
4 5 8
5 1 2
5 2 5
5 3 6
5 4 5

Sample Output 3

517

update @ 2024/3/10 09:22:01