Balkan OI '11 P5 - Time Is Money

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
Balkan Olympiad in Informatics: 2011 Day 2, Problem 2

The NetLine company wants to offer broadband internet to N towns. For this, it suffices to construct a network of N-1 broadband links between the towns, with the property that a message can travel from any town to any other town on this network. NetLine has already identified all pairs of towns between which a direct link can be constructed. For each such possible link, they know the cost and the time it would take to construct the link.

The company is interested in minimizing both the total amount of time (links are built one at a time) and the total amount of money spent to build the entire network. Since they couldn't decide among the two criteria, they decided to use the following formula to evaluate the value of a network:

\text{SumTime} = sum of times spent to construct the chosen links
\text{SumMoney} = sum of the money spent to construct the chosen links
V = \text{SumTime} \times \text{SumMoney}


Find a list of N-1 links to build, which minimizes the value V.

Input Specification

The first line of input contains integers N – the number of towns and M – the number of pairs of towns which can be connected. The towns are numbered starting from 0 to N-1. Each of the next M lines contain four integers x, y, t and c – meaning town x can be connected to town y in time t and with cost c.

Sample Output

In the first line of output print two numbers: the total time (SumTime) and total money (SumMoney) used in the optimal solution (the one with minimal value V), separated by one space. The next N-1 lines describe the links to be constructed. Each line contains a pair of numbers (x,y) describing a link to be build (which must be among the possible links described in the input file). The pairs can be printed out in any order. When multiple solutions exist, you may print any of them.


  • 1 \le N \le 200
  • 1 \le M \le 10\,000
  • 0 \le x,y \le N-1
  • 1 \le t,c \le 255
  • One test has M = N - 1
  • 40\% of the tests will have for each possible link t = c

Sample Input

5 7
0 1 161 79
0 2 161 15
0 3 13 153
1 4 142 183
2 4 236 80
3 4 40 241
2 1 65 92

Sample Output

279 501
2 1
0 3
0 2
3 4


  • 3
    dolphingarlic  commented on July 22, 2020, 2:27 p.m. edited

    Isn't this problem actually from Balkan OI 2011?

    • 4
      Kirito  commented on July 22, 2020, 7:51 p.m.

      You are correct (this is why the problems were renamed and moved around; having 4 BOIs is extremely confusing)