CCO '20 P3 - Mountains and Valleys

View as PDF

Submit solution

Points: 35 (partial)
Time limit: 7.0s
Memory limit: 1G

Author:
Problem type
Canadian Computing Olympiad: 2020 Day 1, Problem 3

You are planning a long hiking trip through some interesting, but well-known terrain. There are N interesting sites you would like to visit and M trails connecting pairs of sites. Each trail has a difficulty level indicated as a positive integer.

The trail system is a bit special, however. There are exactly N1 trails with difficulty level 1 (these are completely flat trails), and the rest of the trails all have a difficulty level of at least N3 (these are very mountainous trails). (The ceiling of x, denoted as x, is the smallest integer greater than or equal to x.)

Additionally, it is possible to travel between any two sites using only the trails with difficulty level 1.

You would like to visit every site, starting your walk from any site of your choice and finishing at some other site, such that you visit each site at least once and the total sum of difficulty levels is minimum among all walks. Note that walking a trail k times with difficulty level d contributes a value of kd to the sum of difficulty levels.

Input Specification

The first line of input contains two space-separated integers N (4N500000) and M (N1M2000000).

The next M lines contain three space-separated integers xi, yi, and wi, describing the trail between sites xi and yi with difficulty level wi (1iM;0xi,yiN1;xiyi). Note that there is at most one trail between every pair of sites, and that wi=1 or N3wiN.

For 1 of the 25 marks available, N6 and M10.

For an additional 2 of the 25 marks available, N20 and M40.

For an additional 2 of the 25 marks available, N5000, M10000, and either wi=1 or N2wiN.

For an additional 6 of the 25 marks available, N100 and M200.

For an additional 2 of the 25 marks available, N500 and M1000.

For an additional 3 of the 25 marks available, N5000 and M10000.

For an additional 5 of the 25 marks available, N80000 and M160000.

Output Specification

Output one integer, which is the minimum sum of difficulty levels taken for all trails walked to visit each site once.

Sample Input

Copy
9 10
0 1 1
0 2 1
0 3 1
1 4 1
2 5 1
2 6 1
3 7 1
3 8 1
2 4 5
6 7 3

Sample Output

Copy
11

Explanation of Output for Sample Input

This is the set of flat trails.

This is the entire set of trails with all the difficulty levels.

This is the entire set of trails, with trails with difficulty level 1 being omitted.

An optimal walk for this set of trails is 4102526738 with a total cost of 1+1+1+1+1+1+3+1+1=11. There is no way to make a walk that visits all the sites at least once with a lower total difficulty level cost.


Comments

There are no comments at the moment.