PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000087. Tổng khoảng cách sau từng pha Floyd (Shortest Path Queries 2)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng khoảng cách sau từng pha Floyd (Shortest Path Queries 2)

Shortest Path Queries 2

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given a directed positive-weighted graph, Floyd processes intermediate vertices 1,2,…,n1,2,\ldots,n in that order. After each phase sum finite shortest distances for every ordered pair and add this phase sum to a global answer. Print the sum over all phases.

Input

First n,mn,m, then mm directed positive-weight edges $u v w` using 1-based vertices. Parallel edges are allowed.

Output

Print a single 64-bit integer.

Subtasks

  • Subtask 1 (40 points): 2≤n≤72\le n\le7. 0≤m≤10000\le m\le1000, 1≤w≤1061\le w\le10^6; có thể có cạnh song song, cung có hướng.

  • Subtask 2 (60 points): 2≤n≤302\le n\le30. 0≤m≤10000\le m\le1000, 1≤w≤1061\le w\le10^6; có thể có cạnh song song, cung có hướng.

Examples

Input

3 3
1 2 1
2 3 2
1 3 10

Output

25

Explanation

Phase 1 contributes 13. Using vertex 2 as an intermediate reduces d(1,3) from 10 to 3, so phases 2 and 3 contribute 6 each; grand total 25.