USACO Gold 2020 January - Time is Mooney
Authors: Nathan Gong, Ryan Chou, David Guo
Explanation
We define as the maximum moonies Bessie can have at city on day . Starting with , we iterate over each day up to , the maximum number of days, and over each city, updating for all cities reachable from city via a directed road.
We compare the current value of with the value of coming from an adjacent city on the previous day, , and take the maximum. After processing each day, we calculate the profit as , representing the total moonies minus the travel cost for days, and track the maximum profit.
Note that the maximum amount of money Bessie makes is in the case where she makes moonies per city and the cost is mooney. This is also because the edge weights are bounded by moonies. Her earnings become negative when , so we only have to check her movement across the cities for at most days.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;const int MAX_DAYS = 1000; // Maximum number of days Bessie can travelint main() {ifstream read("time.in");int n, m, c;read >> n >> m >> c;
Java
import java.io.*;import java.util.*;public class Main {static final int MAX_DAYS = 1000; // Maximum number of days Bessie can travelpublic static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("time.in"));PrintWriter pw =new PrintWriter(new BufferedWriter(new FileWriter("time.out")));
Python
with open("time.in", "r") as fin:n, m, c = map(int, fin.readline().split())moonies = list(map(int, fin.readline().split()))adj = [[] for _ in range(n)]for _ in range(m):a, b = map(int, fin.readline().split())adj[a - 1].append(b - 1) # Convert 1-indexed input to 0-indexedMAX_DAYS = 1000 # Maximum number of days Bessie can travel
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!