Table of Contents

ExplanationImplementation

Solution Outline

Explanation

First, we binary search on the initial velocity.

Then, we use dynamic programming on bitmasks to check whether an initial velocity is valid. Let dp[i][j]\texttt{dp}[i][j] be the minimum possible time to reach visit every mice in subset ii with jj as the last visited mouse. If it is not possible to reach subset ii with jj as the last visited mice, let dp[i][j]=\texttt{dp}[i][j] = \infty.

When transitioning, iterate through every mouse not in the current subset and try adding it to the current subset.

Finally, an initial velocity is possible iff there is an index ii such that dp[2n1][i]\texttt{dp}[2^{n}-1][i] \neq\infty.

Implementation

Time Complexity: O(log(M)N22N)\mathcal{O}(\log(M)N^2\cdot2^N), where MM is the maximum possible velocity.

C++

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 15;
const long double PRECISION = 1e-3, INF = 1e18;
long double dp[1 << MAX_N][MAX_N], p[MAX_N]; // p[i] = pow(speed_red, i)
struct Mouse {
long double x, y, time;

Java

import java.io.*;
import java.util.*;
public final class CatAndMice {
private static final long PRECISION = (long)1e4;
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int miceNum = Integer.parseInt(read.readLine());
int[][] mice = new int[miceNum][];
for (int m = 0; m < miceNum; m++) {

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!