#P9481. [NOI2023] 贸易
[NOI2023] 贸易
Problem Description
In recent years, Country A’s commerce has grown rapidly, but the construction of domestic roads has failed to keep up, which has clearly become a restriction on people’s trade activities. The administrators have put a lot of effort into this.
Specifically, Country A has a total of cities, where city is the capital. For every non-capital city , there is a directed road starting from city and going to city . For convenience, call such roads “Type-1 roads”, and call city the “parent city” of city .
In addition, there are directed roads. Suppose the -th road starts from city and goes to city . Such roads have a special property: starting from city and repeatedly moving along Type-1 roads to the “parent city”, you can always eventually reach city . Call such roads “Type-2 roads”.
Each road has a corresponding length. Therefore, for any two cities and in Country A, we can compute the minimum possible sum of road lengths along a route that starts from city and follows roads to reach city . Denote this value by . However, due to serious flaws in Country A’s road system, it may be impossible to reach city from city . In this case, define . Also, traveling from a city to itself requires no roads, so define .
Now the administrators want to compute these values in order to reasonably measure how convenient trade is. But because there are too many cities, listing all these values one by one would be far too much work. Therefore, they only want the sum of all , i.e. , and they ask you to help.
Input Format
The first line contains two positive integers and .
The second line contains positive integers. The -th number denotes the length of the “Type-1 road” starting from city and going to city .
The next lines each contain three positive integers , describing a “Type-2 road” from city to city , with length .
Output Format
Output one line with one positive integer, the corresponding answer. Since the answer may be very large, you only need to output it modulo .
2 1
2 1
1 2 2
8
见附件中的 trade/trade2.in。
见附件中的 trade/trade2.ans。
见附件中的 trade/trade3.in。
见附件中的 trade/trade3.ans。
见附件中的 trade/trade4.in。
见附件中的 trade/trade4.ans。
Hint
Constraints
For all testdata, it is guaranteed that: , , , .
::cute-table{tuack}
| Test Point ID | Whether There Is a Special Property | ||
|---|---|---|---|
| No | |||
| ^ | |||
| ^ | |||
| Yes | |||
| ^ | No | ||
| Yes | |||
| ^ | No | ||
Special property: It is guaranteed that every “Type-2 road” starts from the capital (city ).
Translated by ChatGPT 5