#P1073. [NOIP 2009 提高组] 最优贸易

    ID: 73 远端评测题 1000ms 128MiB 尝试: 1 已通过: 1 难度: 7 上传者: 标签>动态规划,dp搜索图论2009NOIp 提高组最短路强连通分量,缩点

[NOIP 2009 提高组] 最优贸易

Description

Country CC has nn major cities and mm roads, each connecting a pair of these nn cities. Between any two cities, there is at most one direct road. Among these mm roads, some are one-way and some are two-way. A two-way road is still counted as 11 road.

Because country CC is vast and resources vary by region, the price of the same commodity may differ between cities. However, the buy price and the sell price of the same commodity are always identical within the same city.

A merchant, A-Long, is traveling in country CC. After learning that the price of the same commodity may differ between cities, he decides to earn some travel money from price differences while touring. The cities in country CC are labeled from 11 to nn. A-Long will start from city 11 and eventually end his trip in city nn. During the trip, he may pass through any city multiple times, and he is not required to visit all nn cities. He will earn travel money in this way: he chooses a city he passes to buy his favorite commodity, a crystal ball, and later sells it in another city he passes, using the price difference as his travel funds. Since A-Long mainly came to travel, he will perform this trade at most once. If there is no profitable opportunity, he can choose not to trade.

Suppose there are 55 major cities in country CC, with the city indices and road connections shown below. A single arrow indicates a one-way road, and a double arrow indicates a two-way road.

Assume the crystal ball prices in cities 1n1 \sim n are 4,3,5,6,14, 3, 5, 6, 1.

A-Long can choose the route 12351 \to 2 \to 3 \to 5, buy a crystal ball in city 22 at price 33, and sell it in city 33 at price 55, earning 22.

A-Long can also choose the route 145451 \to 4 \to 5 \to 4 \to 5, buy a crystal ball the first time he reaches city 55 at price 11, and sell it the second time he reaches city 44 at price 66, earning 55.

Given the crystal ball prices of nn cities and the information of mm roads (the indices of the two cities connected by each road and whether the road is one-way or two-way), please tell A-Long the maximum travel money he can earn.

Input Format

The first line contains 22 positive integers nn and mm, separated by a space, representing the number of cities and the number of roads.

The second line contains nn positive integers, separated by spaces, in index order, representing the commodity prices of the nn cities.

The following mm lines each contain 33 positive integers x,y,zx, y, z, separated by spaces. If z=1z = 1, the road is a one-way road from city xx to city yy; if z=2z = 2, the road is a two-way road between cities xx and yy.

Output Format

Output a single integer, the maximum travel money that can be earned. If no trade is performed, output 00.

5 5 
4 3 5 6 1 
1 2 1 
1 4 1 
2 3 2 
3 5 1 
4 5 2 
5

Hint

Constraints

  • It is guaranteed that city 11 can reach city nn.
  • For 10%10\% of the testdata, 1n61 \le n \le 6.
  • For 30%30\% of the testdata, 1n1001 \le n \le 100.
  • For 50%50\% of the testdata, there is no tour route that starts from a city and returns to the same city.
  • For 100%100\% of the testdata: 1n1000001 \le n \le 100000, 1m5000001 \le m \le 500000, 1x,yn1 \le x, y \le n, 1z21 \le z \le 2, and city prices 100\le 100.

NOIP 2009 Senior, Problem 3.

Translated by ChatGPT 5