ICPC 2009 - J. Subway Timing
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
Source-first archive entry
This page is built from the copied files in competitive_programming/icpc/2009/J-subway-timing. Edit
competitive_programming/icpc/2009/J-subway-timing/solution.tex to update the written solution and
competitive_programming/icpc/2009/J-subway-timing/solution.cpp to update the implementation.
The website does not replace those files with hand-maintained HTML. It reads the copied source tree during the build and exposes the exact files below.
Problem Statement
Copied statement text kept beside the solution archive for direct reference.
Problem J
Subway Timing
Input: subway.in
Like most modern cities, Stockholm has a well-developed public transportation system. The heart of public
transportation in Stockholm is the subway. A topological map of the subway system illustrates the different
subway lines and how they are connected, as illustrated in Figure 8. For this problem you should assume that a
subway map is always tree-shaped, even though this is not quite true for Stockholm because of the cycle formed
by the green and blue lines.
Figure 8: Stockholm subway map
A topological map says very little about the geometry of a subway system, such as the distances (and
consequently travel times) between different subway stations. For instance, as most students in Stockholm
know, the distance between “Tekniska Högskolan” (The Royal Institute of Technology) and “Universitetet”
(Stockholm University) is quite large, even though there is no indication of this on the map.
You must write a program that can augment a topological map by writing down the time required to travel
between every pair of adjacent subway stations. Fortunately, those travel times are known, so you do not have to
measure the times yourself. But the actual travel times are given in seconds, while the times must be written on
the map as estimates of integral numbers of minutes.
A natural way of estimating times might be to simply round all the travel times to the nearest minute. However,
this can cause huge cumulative errors. For instance, in the Stockholm map, this estimation method could result
in an error as large as 15 minutes in the total travel time between some pairs of stations. In order to counter this,
your program may choose to round some travel times up and round other travel times down. The rounding must
be done in such a way that the largest cumulative error between any pair of stations is minimized.
Input
The input consists of several test cases. Each test case starts with an integer N (1 ≤ N ≤ 100), which is the
number of subway stations. The N stations are identified using the integers from 1 to N. Each of the next N-1
lines contains three integers a, b and t (1 ≤ a, b ≤ N, and 1 ≤ t ≤ 300), indicating that stations a and b are
adjacent and that it takes t seconds to travel between them. For simplicity, ignore the time a train spends
standing still at a station.
The last test case is followed by the integer zero on a line by itself.
Output
For each test case, print the case number (starting with 1) then the largest rounding error in seconds of travel
time between any pair of stations when the times for adjacent pairs of stations are rounded optimally. Follow the
format of the sample output.
Sample Input Output for the Sample Input
2 Case 1: 10
1 2 110 Case 2: 40
4 Case 3: 60
1 2 40
2 3 40
3 4 40
4
1 2 90
1 3 90
1 4 90
0
Editorial
Rendered from the copied solution.tex file. The original TeX source remains
available below.
Key Observations
Write the structural observations that make the problem tractable.
State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
If the constraints matter, explain exactly which part of the solution they enable.
Algorithm
Describe the data structures and the state maintained by the algorithm.
Explain the processing order and why it is sufficient.
Mention corner cases explicitly if they affect the implementation.
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
State the first key claim.
Proof.
Provide a concise proof.
Lemma 2.
State the next claim if needed.
Proof.
Provide a concise proof.
Theorem.
The algorithm outputs the correct answer for every valid input.
Proof.
Combine the lemmas and finish the argument.
Complexity Analysis
State the running time and memory usage in terms of the input size.
Implementation Notes
Mention any non-obvious implementation detail that is easy to get wrong.
Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
Code
Exact copied C++ implementation from solution.cpp.
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Source Files
Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}
\title{ICPC World Finals 2009\\J. Subway Timing}
\author{}
\date{}
\begin{document}
\maketitle
\section*{Problem Summary}
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
\section*{Key Observations}
\begin{itemize}[leftmargin=*]
\item Write the structural observations that make the problem tractable.
\item State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
\item If the constraints matter, explain exactly which part of the solution they enable.
\end{itemize}
\section*{Algorithm}
\begin{enumerate}[leftmargin=*]
\item Describe the data structures and the state maintained by the algorithm.
\item Explain the processing order and why it is sufficient.
\item Mention corner cases explicitly if they affect the implementation.
\end{enumerate}
\section*{Correctness Proof}
We prove that the algorithm returns the correct answer.
\paragraph{Lemma 1.}
State the first key claim.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Lemma 2.}
State the next claim if needed.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.
\paragraph{Proof.}
Combine the lemmas and finish the argument.
\section*{Complexity Analysis}
State the running time and memory usage in terms of the input size.
\section*{Implementation Notes}
\begin{itemize}[leftmargin=*]
\item Mention any non-obvious implementation detail that is easy to get wrong.
\item Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
\end{itemize}
\end{document}
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}