#P1261. [CERC2002] 服务器储存信息问题

[CERC2002] 服务器储存信息问题

Description

The Kingdom of Byteland is preparing to build a large network among its servers and provide various services.

The network consists of nn servers, connected by bidirectional links. There is at most one direct link between any pair of servers. Each server is directly connected to at most 1010 servers. Furthermore, there is a path between any two servers.

Each transmission link has a fixed length. Let δ(v,w)\delta(v,w) denote the length of the shortest path between servers vv and ww, and for any vv we have δ(v,v)=0\delta(v,v)=0.

Some servers provide more services than others and are therefore more important. We use r(v)r(v) to denote the importance rank\texttt{rank} of server vv. A higher rank\texttt{rank} means a more important server.

Each server stores information about nearby servers. Of course, not all servers’ information is stored—only the interesting ones. Server vv is interested in server ww if there does not exist a server uu such that r(u)>r(w)r(u)>r(w) and δ(v,u)δ(v,w)\delta(v,u)\le\delta(v,w).

For example, all servers with the highest rank\texttt{rank} are interesting to every other server. If vv has the highest rank\texttt{rank}, then since δ(v,v)=0\delta(v,v)=0, vv is interested only in servers with the highest rank\texttt{rank}.

We define B(v)B(v) as the set of servers that server vv is interested in. We want to compute the total amount of information stored by all servers, i.e., the sum of B(v)|B(v)| over all servers. Byteland does not want to store a large amount of data, so the total amount of stored data (the sum of B(v)|B(v)|) will not exceed 30n30n.

Your task is to write a program that reads the network layout of Byteland and computes the total amount of information stored by all servers.

Input Format

The first line contains two integers nn and mm. Here nn is the number of servers and mm is the number of transmission links.

The next nn lines each contain one integer. The integer on the ii-th line is r(i)r(i), the rank\texttt{rank} of server ii.

The next mm lines each describe a transmission link with three integers a,b,ta,b,t. Here aa and bb are the indices of the two servers connected by the link, and tt is the length of the link.

Output Format

Output a single integer: the total amount of data stored by all servers, i.e., the sum of B(v)|B(v)|.

4 3
2
3
1
1
1 4 30
2 3 20
3 4 20
9

Hint

Output explanation:

$B(1)=\{1,2\},B(2)=\{2\},B(3)=\{2,3\},B(4)=\{1,2,3,4\}$.

Constraints:

1n30000,1m5n1\le n\le30000,1\le m\le5n

1r(i)101\le r(i)\le 10

1t1000,1a,bn,ab1\le t\le 1000,1\le a,b\le n,a\neq b.

Translated by ChatGPT 5