Baltic OI 2017 - Toll
Author: Andi Qu
Solution
Split the graph into "layers" with nodes each. Notice how this graph somewhat resembles a neural network.
Let denote the minimum cost of a path between nodes to .
For any triple , the following recurrence holds:
(Similar to the Floyd-Warshall recurrence).
To finish, we can use any algorithm that allows us to quickly answer static range queries (ex. divide & conquer). Here we'll use a sparse table. Instead of storing each DP state, we can store only for each , , , and . Then we can find the value of in time per query with binary jumping. This gives a time complexity of .
Code
1#include <bits/stdc++.h>2using namespace std;34int k, n, m, o;5int dp[50000][17][5][5], ans[5][5], tmp[5][5];67void combine(int target[5][5], int a[5][5], int b[5][5]) {8 for (int x = 0; x < k; x++) {9 for (int y = 0; y < k; y++) {10 for (int z = 0; z < k; z++) {
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!