JOI 2021 - Robot
Author: Andi Qu
Explanation
Lemma: We can always recolour an edge so that its colour is different from those of the other edges incident to and .
Proof: If we take away , then there are at most edges incident to and . Since there are colours in total, there must exist some colour such that none of those edges have it (by the pigeonhole principle). Therefore, we can simply recolour to that colour.
This means we can define the cost to traverse an edge as:
It may be tempting to use Dijkstra's algorithm at this point, but that will unfortunately result in a wrong answer. Consider the following:
- The robot goes on the path .
- and have the same colour.
- It's optimal to recolour when going , but it's optimal to recolour the same-coloured neighbours of when going .
In this case, using Dijkstra's algorithm naively will count the cost to recolour twice in our answer!
To amend this, let's define two DP arrays instead of just one:
- is the minimum cost to get to node .
- is the minimum cost to get to node if:
- The robot just traversed a -coloured edge to get to node .
- We're meant to have recoloured that edge, but haven't yet.
- The robot will definitely leave node via another -coloured edge and we will recolour all of its same-coloured neighbours.
We can then use Dijkstra's algorithm and some casework to solve this problem in amortized time.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>typedef long long ll;using namespace std;const ll INF = 1e18;struct Edge {int to, c;ll p;};
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!