atcoder#ABC208D. [ABC208D] Shortest Path Queries 2
[ABC208D] Shortest Path Queries 2
Score : points
Problem Statement
There are cities and roads in the Kingdom of Takahashi.
The cities are numbered through , and the roads are numbered through . Road is a one-way road that leads from City to City , and it takes minutes to go through it.
Let us define as the answer to the following query.
- Compute the minimum time needed to get from City to City . Here, other than the Cities and , it is only allowed to pass through Cities through . If City is unreachable or , the answer should be .
Compute for all triples 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
- or , if .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
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 such that are as follows.
- For : .
- For : .
- For : .
3 0
0
We have for all .
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