#P15805. [GESP202603 八级] 子图最短路

    ID: 17870 远端评测题 1000ms 512MiB 尝试: 6 已通过: 4 显示难度普及+/提高− 上传者: 标签>图论最短路2026GESPFloyd 算法

[GESP202603 八级] 子图最短路

Background

Related multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1212.

Problem Description

Given a weighted undirected graph GG with nn nodes and mm edges, the nodes are numbered 1,2,,n1, 2, \dots, n in order. The ii-th edge (1im1 \le i \le m) connects two nodes numbered uiu_i and viv_i, with weight wiw_i.

For given 1rn1 \le \ell \le r \le n, construct a subgraph G(,r)G(\ell, r) of GG as follows:

  • Keep the nodes in GG whose numbers are in the interval [,r][\ell, r]. Delete all other nodes whose numbers are not in [,r][\ell, r] and the edges incident to them. The remaining nodes and edges form the subgraph G(,r)G(\ell, r).

For any nodes u,vu, v in G(,r)G(\ell, r), we have u,vr\ell \le u, v \le r. Let the shortest distance between uu and vv in the subgraph G(,r)G(\ell, r) be d(,r,u,v)d(\ell, r, u, v). In particular, if uu and vv are not connected in G(,r)G(\ell, r), then define d(,r,u,v)=0d(\ell, r, u, v) = 0.

You need to compute $\sum_{\ell=1}^{n} \sum_{r=\ell}^{n} \sum_{u=\ell}^{r} \sum_{v=u}^{r} d(\ell, r, u, v)$ modulo 10910^9.

  • In this problem, the English letter ll is written as \ell to avoid confusion between the letter ll and the digit 11.

Input Format

The first line contains two positive integers n,mn, m, representing the number of nodes and the number of edges.

The next mm lines: the ii-th line (1im1 \le i \le m) contains three positive integers ui,vi,wiu_i, v_i, w_i, representing an edge connecting nodes uiu_i and viv_i with weight wiw_i.

Output Format

Output one line with one integer, representing $\sum_{\ell=1}^{n} \sum_{r=\ell}^{n} \sum_{u=\ell}^{r} \sum_{v=u}^{r} d(\ell, r, u, v)$ modulo 10910^9.

3 2
1 2 1
2 3 2
9
4 6
1 2 100
2 3 100
3 4 100
1 3 10
2 4 10
1 4 1
784

Hint

For 40%40\% of the testdata, it is guaranteed that 2n202 \le n \le 20.

For all testdata, it is guaranteed that 2n1002 \le n \le 100, 2mn(n1)22 \le m \le \frac{n(n-1)}{2}, 1ui,vin1 \le u_i, v_i \le n, and 1wi1061 \le w_i \le 10^6. There may be multiple edges in the graph.

Translated by ChatGPT 5