Editorial for EGOI '23 P4 - Bikes vs Cars
Submitting an official solution before solving the problem yourself is a bannable offence.
Problem author: Nils Gustafsson.
Solution
We can start by writing down what the numbers and
mean in a more
concise way:
Here, the is taken over all paths from
to
, and the
is over all bike
lane widths of edges on the path.
Note that if we let , then we get rid of the parameter
, because
So now we only focus on the bike lane width, and our goal is to construct a graph
that satisfies all the - and
-constraints imposed by the numbers
and
.
Subtask 1 and 2
Here it is helpful to make the following observation:
Observation 1: The minimum edge weight in the graph is , and the
maximum edge weight is
.
In this subtask, this implies that if , then it is impossible.
Otherwise, we can add edges of weight and
that connect all pairs of
vertices.
Subtask 3 (
)
To solve the case when is small, we need to find a construction that always
works, but uses too many edges.
Observation 2: If there is a valid solution, then the following construction will
also work: disregard all pairs of vertices where . For all other pairs
of vertices, add edges of weight
and
.
Here is some motivation why this works:
First, if we have an edge between and
of weight
, then
and
, which means that
. So we can never have an edge between
two vertices if
. This means that if a graph constructed as above is
disconnected, then there is no solution.
Second, a path in the construction that minimizes the maximum weight between
and
will only use edges
that we added, since they are always smaller
than the
-edges. But it could happen that the minimum maximum weight
ends up smaller than
, if there is a path
such that
However, if such a path existed, then it could also be used in any other construction,
so in this case the answer should be NO
anyway.
Third, similar arguments can be made about the -edges.
So all we have to do to get this subtask is to construct the graph above, and check that it is a valid solution. This check can be done by running an algorithm similar to Floyd-Warshall's, or by using minimum spanning trees.
Subtask 5 (all
are the same)
Let be the value of all
. Remember from subtask 1 that the maximum
weight in the graph is the maximum value of
, which is
. So in this subtask,
we must have that
, otherwise there is no solution. But if this
is the case, then we can connect all vertices with edges of weight
and then
forget about the
constraints.
After that, we only have to focus on the numbers .
Observation 3: If we have a graph that satisfies all -constraints, then the
minimum spanning tree of that graph will still satisfy all the
-constraints.
This fact is a rather standard trick when it comes to minimum spanning trees.
To see why it is true, assume that there exists a path from to
such that the
maximum weight is smaller than the maximum weight of the path along the
minimum spanning tree. Then we could remove the largest weight edge on the
path along the tree, and replace it with an edge of smaller weight. This would
create a smaller spanning tree, which is a contradiction.
So, to solve the subtask, find a minimum spanning tree of the complete graph
whose edge weights are , and check that it is a valid solution. There are
several algorithms to efficiently find a minimum spanning tree, like Prim's or
Kruskal's.
Full score
To get full score, we will put together the things we learned in the previous subtasks.
First, take the construction from Subtask 3. Like we saw in Subtask 5, we can
take a minimum spanning tree of this graph, and it will still satisfy the -constraints.
Similarly, we can take the maximum spanning tree to satisfy the
-constraints. Furthermore, if we take the union of these two trees, then
we get a solution, if one exists.
To see why this works, note that the minimum spanning tree will only use the
-edges added in Subtask 3, and the maximum spanning tree will only use the
-edges. Also, a path between
and
that minimizes the maximum weight
will only use the edges from the MST, like we saw in Subtask 5, and this value
is exactly
like we want (and similarly for
).
Summary of how to get 100 points: Create a graph with edges of weight
and
between every pair of vertices such that
, take the union of
the minimum and maximum spanning tree, and finally check that this is a valid
solution.
Comments