atcoder#ABC208D. [ABC208D] Shortest Path Queries 2

[ABC208D] Shortest Path Queries 2

Score : 400400 points

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)$.

3 2
1 2 3
2 3 2
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.
3 0
0

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

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
517