UTS Open '15 #5 - Distribution Channel

View as PDF

Submit solution


Points: 20 (partial)
Time limit: 3.0s
Memory limit: 256M

Author:
Problem types
Distribution channels are often very confusing.

After acquiring enough funding and finally finishing the development for her product, Mrs. Evans wants to release it to the local market. However, the Canadian Competition Act prevents her from choosing the cheapest way to connect her stores.

In order to spend the least amount of money and abide by the Canadian Competition Act, she must find the second cheapest way to connect her stores.

Mrs. Evans hopes to open N (1N50000) stores and there are M (1M100000) possible connections between her stores. The ith connection connects station ai to station bi with a cost of ci. She can only choose N1 connections.

If it is impossible for her to do this, output -1.

N.B. The time limit is rather strict for certain slow languages like Python and Java. C++ is recommended for this question.

Partial Marks

For 25% of the marks, you may assume that N5000, M10000.

For 50% of the marks, you may assume that N10000, M50000.

Input Specification

The first line will contain N, and M. The ith of the next M lines will contain ai, bi, and ci, where (1ai,biN,aibi,1ci10000).

Output Specification

A single line containing the second cheapest way to connect her stores.

Sample Input 1

Copy
3 3
1 2 1
2 3 2
3 1 3

Sample Output 1

Copy
4

Explanation for Sample Output 1

Mrs. Evans chooses the following connections for a total cost of 4:

  • 12 with cost 1
  • 13 with cost 3

Sample Input 2

Copy
5 10
2 3 5
1 5 5
1 2 3
1 3 2
5 3 2
4 3 3
4 5 9
4 2 10
5 2 4
4 1 9

Sample Output 2

Copy
11

Explanation for Sample Output 2

Mrs. Evans chooses the following connections for a total cost of 11:

  • 13 with cost 2
  • 35 with cost 2
  • 34 with cost 3
  • 25 with cost 4

Comments


  • 1
    bobhob314  commented on Feb. 11, 2015, 9:47 p.m.

    When you say if it is possible, I think you mean the opposite.


    • -1
      bobhob314  commented on Feb. 11, 2015, 9:48 p.m.

      Relevant: In re Sample Input 2, CAN IT BE?