Omnes Viae Yokohamam Ducunt?
InterviewTime limit3sMemory limit2048 MB
Find a spanning tree rooted at city 1 that minimizes the sum over edges of vulnerability times the significance total of the cities cut off from city 1 by that edge.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Heap
- Solved
- No attempts yet
Statement
Time limit: 3000 ms. Memory limit: 2048 MB.
"Omnes viae Romam ducunt" is an old Latin proverb meaning "all roads lead to Rome." It is still desirable to have access to the capital from all the regions of a country.
The Kingdom of Kanagawa has a number of cities, including the capital city, Yokohama. The Ministry of Transport of the Kingdom is now planning to construct a highway network that connects all those cities.
There are several candidate highway segments, and each one directly connects two cities. A highway network is a set of segments chosen from the candidates. The network must satisfy the following conditions.
- All the cities must be connected via highway segments in the network, directly or indirectly.
- The number of segments must be minimal to save the budget. In other words, the network must not be redundant, so the path between any two cities is unique.
Access to Yokohama matters most. Because the network has no redundancy, if a natural disaster makes one segment unavailable, some cities become inaccessible from Yokohama.
Each city has a significance value based on its population and economic scale. When a natural disaster hits one segment of a given network, the damage is the sum of the significance values of the cities that become inaccessible from Yokohama. The vulnerability of each candidate segment is also assessed. The risk severity of a segment is the product of its damage and its vulnerability. The total risk severity of a network is the sum of the risk severities of all its segments. Find the minimum total risk severity.
Input
The first line contains two integers and (, ), the numbers of cities and candidate segments. The cities are numbered from to , and Yokohama is city .
The second line contains integers , where each () is the significance value of city .
The next lines describe the candidate segments. Line contains three integers , , and , meaning the candidate segment between cities and has vulnerability (, ). Each pair appears at most once.
It is guaranteed that a highway network connecting all the cities can be built from some of these segments.
Output
Print the minimum possible total risk severity on one line.