ICPC 2010 - D. Castles
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/2010/D-castles. Edit
competitive_programming/icpc/2010/D-castles/solution.tex to update the written solution and
competitive_programming/icpc/2010/D-castles/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 D
Castles
Problem ID: castles
Wars have played a significant role in world history. Unlike modern wars, armies in the middle ages were
principally concerned with capturing and holding castles, the private fortified residences of lords and nobles.
The size of the attacking army was an important factor in an army’s ability to capture and hold one of these
architectural masterpieces.
Figure 2
A certain minimum number of soldiers were required to capture a castle. Some soldiers were expected to die
during the attack. After capturing the castle, some soldiers were required to remain in the castle to defend it
against attacks from another enemy. Of course, those numbers were different for different castles. Commanders
of the armies were obliged to consider the number of soldiers required for victory. For example, there are five
castles in the region map shown in Figure 2. The castle at the lower right requires at least 20 soldiers to wage a
winning attack. None are expected to perish during the attack, and 10 soldiers must be left in the castle when the
army moves on.
In this problem you must determine the minimum size of an army needed to capture and hold all the castles in a
particular region. For reasons of security, there is exactly one (bi-directional) route between any pair of castles
in the region. Moving into the neighborhood of an uncaptured castle begins an attack on that castle. Any castle
can serve as the first castle to be attacked, without regard for how the army got there. Once any castle has been
captured, the requisite number of soldiers is left in the castle to defend it, and the remainder of the army moves
on to do battle at another castle, if any remain uncaptured. The army may safely pass through the neighborhood
of a castle that it has already captured. But because of the potential for attacks, the army may traverse the route
between a pair of castles no more than twice (that is, at most once in each direction).
Input
The input contains multiple test cases corresponding to different regions. The description of the castles in each
region occupies several lines. The first line contains an integer n ≤ 100 that is the number of castles in the
region. Each of the next n lines contains three integers a, m, and g (1 ≤ a ≤ 1000, 0 ≤ m ≤ a, 1 ≤ g ≤ 1000), that
give the minimum number of soldiers required to successfully attack and capture a particular castle, the number
of soldiers that are expected to die during the attack, and the number of soldiers that must be left at the castle to
defend it. The castles are numbered 1 to n, and the input lines describing them are given in increasing order of
castle numbers. Each of the remaining n – 1 lines in a test case has two integers that specify the castle numbers
of a pair of castles that are connected by a direct route.
A line containing 0 follows the description of the last region.
Output
For each test case, display the case number and the minimum number of soldiers in the army needed to conquer
all the castles in the region. Follow the format shown in the sample output.
Sample Input Output for the Sample Input
3 Case 1: 22
5 5 5 Case 2: 65
10 5 5
5 1 1
1 3
2 3
5
10 5 10
20 10 5
10 10 5
5 5 5
20 0 10
1 2
1 3
1 4
3 5
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 2010\\D. Castles}
\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;
}