From 09c3ee2f24dbfbe51979d30a8f030bd16cd37179 Mon Sep 17 00:00:00 2001 From: David Guo Date: Thu, 5 Dec 2024 22:40:53 -0800 Subject: [PATCH 01/14] Update Time is Mooney Editorial - Revised explanation - Updated C++, Java, and Python implementations - Standardized comments across all implementations - Revised readability and structure of the code --- solutions/gold/usaco-993.mdx | 267 +++++++++++++++++++---------------- 1 file changed, 146 insertions(+), 121 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 33c013e02e..edce900d3d 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -2,14 +2,24 @@ id: usaco-993 source: USACO Gold 2020 January title: Time is Mooney -author: Nathan Gong, Ryan Chou +author: Nathan Gong, Ryan Chou, David Guo --- [Official Analysis (C++)](http://www.usaco.org/current/data/sol_time_gold_jan20.html) ## Explanation -We only need to keep track of the number of days that Bessie has been traveling and the amount of mooney that she has made. For every day up to $T_{\text{max}}$, we'll try to travel to each city, and update the mooney made if the current path is more optimal. The maximum amount of money she makes is $1000t - t^2$, in the case where she makes $1000$ mooney per city and the cost is $1$. Her earnings become negative when $t > 1000$, so we only have to check her movement across the cities for at most $1000$ days. +The problem involves maximizing the profit Bessie can earn while traveling +between cities and collecting moonies. We define $dp[t][i]$ as the maximum +moonies Bessie can have at city $i$ on day $t$. Starting with $dp[0][1] = 0$, we +iterate over each day up to $T_{\text{max}}$, the maximum number of days, and +over each city, updating $dp[t + 1][j]$ for all cities $j$ reachable from city +$i$ via a directed road. We compare the current value of $dp[t + 1][j]$ with the +value of coming from an adjacent city on the previous day, +$dp[t][i] + \text{moonies}[j]$, and take the maximum. After processing each day, +we calculate the profit as $dp[t][1] - C \cdot t^2$, representing the total +moonies minus the travel cost for $t$ days, and track the maximum profit. This +approach ensures we consider all feasible trips and ignore unreachable states. ## Implementation @@ -22,48 +32,55 @@ We only need to keep track of the number of days that Bessie has been traveling #include using namespace std; -const int MAX_DAYS = 1000; - int main() { - freopen("time.in", "r", stdin); - freopen("time.out", "w", stdout); - - int n, m, c; - cin >> n >> m >> c; - - vector earn(n); - for (int i = 0; i < n; i++) { cin >> earn[i]; } - - vector> adj(n); - for (int i = 0; i < m; i++) { - int u, v; - cin >> u >> v; - adj[--u].push_back(--v); - } - - // dp[i][j] = the max money that Bessie can make on day i if she ends in - // city j - vector> dp(MAX_DAYS + 1, vector(n, -1)); - // base case: if Bessie doesn't travel at all, she makes $0 - dp[0][0] = 0; - - int ans = 0; - for (int d = 0; d < MAX_DAYS; d++) { - for (int i = 0; i < n; i++) { - // if dp[d][i] == -1 then the city can't be visited - if (dp[d][i] != -1) { - for (int u : adj[i]) { - /* - * dp[d + 1][u] = max(current money earned, - * previous city's earnings + current city's earnings) - */ - dp[d + 1][u] = max(dp[d + 1][u], dp[d][i] + earn[u]); - } - } - } - ans = max(ans, (dp[d][0] - (c * d * d))); - } - cout << ans << "\n"; + ifstream cin("time.in"); + ofstream cout("time.out"); + + int n, m, c; + cin >> n >> m >> c; + + vector moonies(n + 1); + for (int i = 1; i <= n; i++) { + cin >> moonies[i]; + } + + vector> adj(n + 1); + for (int i = 0; i < m; i++) { + int a, b; + cin >> a >> b; + adj[a].push_back(b); + } + + const int MAX_T = 1000; // Maximum number of days Bessie can travel + + // DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on day t + vector> dp(MAX_T + 1, vector(n + 1, -1)); + dp[0][1] = 0; // Base case: Start at city 1 on day 0 with 0 moonies + + int res = 0; + + for (int t = 0; t <= MAX_T; t++) { + for (int i = 1; i <= n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { + continue; + } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj[i]) { + if (t + 1 <= MAX_T) { + // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // After processing all cities for day t, calculate the profit if Bessie returns to city 1 + res = max(res, dp[t][1] - c * t * t); + } + + cout << res << "\n"; + return 0; } ``` @@ -75,55 +92,61 @@ import java.io.*; import java.util.*; public class TimeIsMooney { - public static void main(String[] args) throws IOException { - Kattio io = new Kattio("time"); - - // max time that we check (it can be any arbitrary large number as long - // as it is sufficient for O(T_MAX * (N + M)) time) - final int T_MAX = 1001; - int n = io.nextInt(); - int m = io.nextInt(); - int c = io.nextInt(); - - // amount of moonies Bessie can make at each city - int[] values = new int[n]; - for (int i = 0; i < values.length; i++) { values[i] = io.nextInt(); } - - List> adj = new ArrayList<>(); - for (int i = 0; i < n; i++) adj.add(new ArrayList<>()); - for (int i = 0; i < m; i++) { - int n1 = io.nextInt() - 1; - int n2 = io.nextInt() - 1; - adj.get(n1).add(n2); - } - - // dp[t][i] = max amount of moonies Bessie can make after t days, - // ending at city i - long[][] dp = new long[T_MAX][n]; - for (int i = 0; i < T_MAX; i++) { Arrays.fill(dp[i], -1); } - - long best = 0; - dp[0][0] = 0; - for (int t = 0; t < T_MAX - 1; t++) { // populate dp array up to T_MAX - for (int i = 0; i < n; i++) { // check each city at each time step - // skip city if it can't be reached at time t - if (dp[t][i] != -1) { - // visit all cities adjacent to i, update dp array - for (int neighbor : adj.get(i)) { - dp[t + 1][neighbor] = - Math.max(dp[t + 1][neighbor], dp[t][i] + values[neighbor]); - } - } - } - // update the max profit Bessie can make after t days, ending at city 0 - best = Math.max(best, dp[t][0] - t * t * c); - } - - io.println(best); - io.close(); - } - - // CodeSnip{Kattio} + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new FileReader("time.in")); + PrintWriter pw = new PrintWriter(new FileWriter("time.out")); + + final int MAX_T = 1000; // Maximum number of days Bessie can travel + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + int[] moonies = new int[n + 1]; + st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= n; i++) { + moonies[i] = Integer.parseInt(st.nextToken()); + } + + List> adj = new ArrayList<>(); + for (int i = 0; i <= n; i++) adj.add(new ArrayList<>()); + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int u = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + adj.get(u).add(v); + } + + // DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on day t + long[][] dp = new long[MAX_T + 1][n + 1]; + for (int t = 0; t <= MAX_T; t++) { + Arrays.fill(dp[t], -1); + } + dp[0][1] = 0; // Base case: Start at city 1 on day 0 with 0 moonies + + long res = 0; + + for (int t = 0; t < MAX_T; t++) { + for (int i = 1; i <= n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) continue; + + // Transition: Consider all roads from city i to its neighbors + for (int neighbor : adj.get(i)) { + if (t + 1 <= MAX_T) { + // Update dp[t + 1][neighbor] by considering a transition from an adjacent city on the previous day + dp[t + 1][neighbor] = Math.max(dp[t + 1][neighbor], dp[t][i] + moonies[neighbor]); + } + } + } + + // After processing all cities for day t, calculate the profit if Bessie returns to city 1 + res = Math.max(res, dp[t][1] - (long) c * t * t); + } + + pw.println(res); + pw.close(); + } } ``` @@ -134,35 +157,37 @@ public class TimeIsMooney { MAX_DAYS = 1000 with open("time.in") as read: - n, m, c = map(int, read.readline().strip().split()) - earn = list(map(int, read.readline().strip().split())) - - adj = [[] for _ in range(n)] - - for _ in range(m): - u, v = map(int, read.readline().strip().split()) - u -= 1 - v -= 1 - adj[u].append(v) - -# dp[i][j] = the max money that Bessie can make on day i if she ends in city j -dp = [[-1] * n for _ in range(MAX_DAYS + 1)] - -# base case: if Bessie doesn't travel at all, she makes $0 -dp[0][0] = 0 -ans = 0 -for d in range(MAX_DAYS): - for i in range(n): - # if dp[d][i] == -1 then the city can't be visited - if dp[d][i] != -1: - for u in adj[i]: - # dp[d + 1][u] = max(current money earned, - # previous city's earnings + current city's earnings) - dp[d + 1][u] = max(dp[d + 1][u], dp[d][i] + earn[u]) - - ans = max(ans, dp[d][0] - (c * d * d)) - -print(ans, file=open("time.out", "w")) + n, m, c = map(int, read.readline().strip().split()) + moonies = [0] + list(map(int, read.readline().strip().split())) + + adj = [[] for _ in range(n + 1)] + for _ in range(m): + u, v = map(int, read.readline().strip().split()) + adj[u].append(v) + +# DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on day t +dp = [[-1] * (n + 1) for _ in range(MAX_DAYS + 1)] +dp[0][1] = 0 # Base case: Start at city 1 on day 0 with 0 moonies + +res = 0 + +for t in range(MAX_DAYS): + for i in range(1, n + 1): + # Skip cities that are unreachable on day t + if dp[t][i] == -1: + continue + + # Transition: Consider all roads from city i to its neighbors + for neighbor in adj[i]: + if t + 1 <= MAX_DAYS: + # Update dp[t + 1][neighbor] by considering a transition from an adjacent city on the previous day + dp[t + 1][neighbor] = max(dp[t + 1][neighbor], dp[t][i] + moonies[neighbor]) + + # After processing all cities for day t, calculate the profit if Bessie returns to city 1 + res = max(res, dp[t][1] - c * t * t) + +with open("time.out", "w") as write: + write.write(f"{res}\n") ``` From bfaf45a2373a451ac581e692249232503240433f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Dec 2024 06:55:30 +0000 Subject: [PATCH 02/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/gold/usaco-993.mdx | 247 ++++++++++++++++++----------------- 1 file changed, 124 insertions(+), 123 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index edce900d3d..9223f13b52 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -33,54 +33,53 @@ approach ensures we consider all feasible trips and ignore unreachable states. using namespace std; int main() { - ifstream cin("time.in"); - ofstream cout("time.out"); - - int n, m, c; - cin >> n >> m >> c; - - vector moonies(n + 1); - for (int i = 1; i <= n; i++) { - cin >> moonies[i]; - } - - vector> adj(n + 1); - for (int i = 0; i < m; i++) { - int a, b; - cin >> a >> b; - adj[a].push_back(b); - } - - const int MAX_T = 1000; // Maximum number of days Bessie can travel - - // DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on day t - vector> dp(MAX_T + 1, vector(n + 1, -1)); - dp[0][1] = 0; // Base case: Start at city 1 on day 0 with 0 moonies - - int res = 0; - - for (int t = 0; t <= MAX_T; t++) { - for (int i = 1; i <= n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) { - continue; - } - - // Transition: Consider all roads from city i to its neighbors - for (int j : adj[i]) { - if (t + 1 <= MAX_T) { - // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); - } - } - } - - // After processing all cities for day t, calculate the profit if Bessie returns to city 1 - res = max(res, dp[t][1] - c * t * t); - } - - cout << res << "\n"; - return 0; + ifstream cin("time.in"); + ofstream cout("time.out"); + + int n, m, c; + cin >> n >> m >> c; + + vector moonies(n + 1); + for (int i = 1; i <= n; i++) { cin >> moonies[i]; } + + vector> adj(n + 1); + for (int i = 0; i < m; i++) { + int a, b; + cin >> a >> b; + adj[a].push_back(b); + } + + const int MAX_T = 1000; // Maximum number of days Bessie can travel + + // DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on + // day t + vector> dp(MAX_T + 1, vector(n + 1, -1)); + dp[0][1] = 0; // Base case: Start at city 1 on day 0 with 0 moonies + + int res = 0; + + for (int t = 0; t <= MAX_T; t++) { + for (int i = 1; i <= n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { continue; } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj[i]) { + if (t + 1 <= MAX_T) { + // Update dp[t + 1][j] by considering a transition from an adjacent + // city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // After processing all cities for day t, calculate the profit if Bessie returns + // to city 1 + res = max(res, dp[t][1] - c * t * t); + } + + cout << res << "\n"; + return 0; } ``` @@ -92,61 +91,61 @@ import java.io.*; import java.util.*; public class TimeIsMooney { - public static void main(String[] args) throws IOException { - BufferedReader br = new BufferedReader(new FileReader("time.in")); - PrintWriter pw = new PrintWriter(new FileWriter("time.out")); - - final int MAX_T = 1000; // Maximum number of days Bessie can travel - StringTokenizer st = new StringTokenizer(br.readLine()); - int n = Integer.parseInt(st.nextToken()); - int m = Integer.parseInt(st.nextToken()); - int c = Integer.parseInt(st.nextToken()); - - int[] moonies = new int[n + 1]; - st = new StringTokenizer(br.readLine()); - for (int i = 1; i <= n; i++) { - moonies[i] = Integer.parseInt(st.nextToken()); - } - - List> adj = new ArrayList<>(); - for (int i = 0; i <= n; i++) adj.add(new ArrayList<>()); - for (int i = 0; i < m; i++) { - st = new StringTokenizer(br.readLine()); - int u = Integer.parseInt(st.nextToken()); - int v = Integer.parseInt(st.nextToken()); - adj.get(u).add(v); - } - - // DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on day t - long[][] dp = new long[MAX_T + 1][n + 1]; - for (int t = 0; t <= MAX_T; t++) { - Arrays.fill(dp[t], -1); - } - dp[0][1] = 0; // Base case: Start at city 1 on day 0 with 0 moonies - - long res = 0; - - for (int t = 0; t < MAX_T; t++) { - for (int i = 1; i <= n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) continue; - - // Transition: Consider all roads from city i to its neighbors - for (int neighbor : adj.get(i)) { - if (t + 1 <= MAX_T) { - // Update dp[t + 1][neighbor] by considering a transition from an adjacent city on the previous day - dp[t + 1][neighbor] = Math.max(dp[t + 1][neighbor], dp[t][i] + moonies[neighbor]); - } - } - } - - // After processing all cities for day t, calculate the profit if Bessie returns to city 1 - res = Math.max(res, dp[t][1] - (long) c * t * t); - } - - pw.println(res); - pw.close(); - } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new FileReader("time.in")); + PrintWriter pw = new PrintWriter(new FileWriter("time.out")); + + final int MAX_T = 1000; // Maximum number of days Bessie can travel + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + int[] moonies = new int[n + 1]; + st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= n; i++) { moonies[i] = Integer.parseInt(st.nextToken()); } + + List> adj = new ArrayList<>(); + for (int i = 0; i <= n; i++) adj.add(new ArrayList<>()); + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int u = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + adj.get(u).add(v); + } + + // DP table: dp[t][i] represents the maximum moonies Bessie can have at city i + // on day t + long[][] dp = new long[MAX_T + 1][n + 1]; + for (int t = 0; t <= MAX_T; t++) { Arrays.fill(dp[t], -1); } + dp[0][1] = 0; // Base case: Start at city 1 on day 0 with 0 moonies + + long res = 0; + + for (int t = 0; t < MAX_T; t++) { + for (int i = 1; i <= n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) continue; + + // Transition: Consider all roads from city i to its neighbors + for (int neighbor : adj.get(i)) { + if (t + 1 <= MAX_T) { + // Update dp[t + 1][neighbor] by considering a transition from + // an adjacent city on the previous day + dp[t + 1][neighbor] = + Math.max(dp[t + 1][neighbor], dp[t][i] + moonies[neighbor]); + } + } + } + + // After processing all cities for day t, calculate the profit if Bessie + // returns to city 1 + res = Math.max(res, dp[t][1] - (long)c * t * t); + } + + pw.println(res); + pw.close(); + } } ``` @@ -157,13 +156,13 @@ public class TimeIsMooney { MAX_DAYS = 1000 with open("time.in") as read: - n, m, c = map(int, read.readline().strip().split()) - moonies = [0] + list(map(int, read.readline().strip().split())) + n, m, c = map(int, read.readline().strip().split()) + moonies = [0] + list(map(int, read.readline().strip().split())) - adj = [[] for _ in range(n + 1)] - for _ in range(m): - u, v = map(int, read.readline().strip().split()) - adj[u].append(v) + adj = [[] for _ in range(n + 1)] + for _ in range(m): + u, v = map(int, read.readline().strip().split()) + adj[u].append(v) # DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on day t dp = [[-1] * (n + 1) for _ in range(MAX_DAYS + 1)] @@ -172,22 +171,24 @@ dp[0][1] = 0 # Base case: Start at city 1 on day 0 with 0 moonies res = 0 for t in range(MAX_DAYS): - for i in range(1, n + 1): - # Skip cities that are unreachable on day t - if dp[t][i] == -1: - continue - - # Transition: Consider all roads from city i to its neighbors - for neighbor in adj[i]: - if t + 1 <= MAX_DAYS: - # Update dp[t + 1][neighbor] by considering a transition from an adjacent city on the previous day - dp[t + 1][neighbor] = max(dp[t + 1][neighbor], dp[t][i] + moonies[neighbor]) - - # After processing all cities for day t, calculate the profit if Bessie returns to city 1 - res = max(res, dp[t][1] - c * t * t) + for i in range(1, n + 1): + # Skip cities that are unreachable on day t + if dp[t][i] == -1: + continue + + # Transition: Consider all roads from city i to its neighbors + for neighbor in adj[i]: + if t + 1 <= MAX_DAYS: + # Update dp[t + 1][neighbor] by considering a transition from an adjacent city on the previous day + dp[t + 1][neighbor] = max( + dp[t + 1][neighbor], dp[t][i] + moonies[neighbor] + ) + + # After processing all cities for day t, calculate the profit if Bessie returns to city 1 + res = max(res, dp[t][1] - c * t * t) with open("time.out", "w") as write: - write.write(f"{res}\n") + write.write(f"{res}\n") ``` From e4b9ce571701ead97debb037f21d04d2b6379787 Mon Sep 17 00:00:00 2001 From: David Guo Date: Fri, 6 Dec 2024 12:39:24 -0800 Subject: [PATCH 03/14] adding suggestions --- solutions/gold/usaco-993.mdx | 295 ++++++++++++++++++----------------- 1 file changed, 152 insertions(+), 143 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 9223f13b52..d0a19eff46 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -9,15 +9,15 @@ author: Nathan Gong, Ryan Chou, David Guo ## Explanation -The problem involves maximizing the profit Bessie can earn while traveling -between cities and collecting moonies. We define $dp[t][i]$ as the maximum -moonies Bessie can have at city $i$ on day $t$. Starting with $dp[0][1] = 0$, we +The problem involves maximizing the profit Bessie can earn while traveling between cities and collecting moonies. In a trip, only two factors matter in evaluating it: the city Bessie is in and the day she is on. We need to explore all paths while efficiently keeping track of the best outcomes. A dynamic programming approach is well-suited for this, as it allows us to break the problem into smaller subproblems: tracking the maximum moonies Bessie can have at each city for every possible day. + +We define $dp[t][i]$ as the maximum moonies Bessie can have at city $i$ on day $t$. Starting with $dp[0][0] = 0$, we iterate over each day up to $T_{\text{max}}$, the maximum number of days, and over each city, updating $dp[t + 1][j]$ for all cities $j$ reachable from city $i$ via a directed road. We compare the current value of $dp[t + 1][j]$ with the value of coming from an adjacent city on the previous day, $dp[t][i] + \text{moonies}[j]$, and take the maximum. After processing each day, -we calculate the profit as $dp[t][1] - C \cdot t^2$, representing the total +we calculate the profit as $dp[t][0] - C \cdot t^2$, representing the total moonies minus the travel cost for $t$ days, and track the maximum profit. This approach ensures we consider all feasible trips and ignore unreachable states. @@ -33,53 +33,55 @@ approach ensures we consider all feasible trips and ignore unreachable states. using namespace std; int main() { - ifstream cin("time.in"); - ofstream cout("time.out"); - - int n, m, c; - cin >> n >> m >> c; - - vector moonies(n + 1); - for (int i = 1; i <= n; i++) { cin >> moonies[i]; } - - vector> adj(n + 1); - for (int i = 0; i < m; i++) { - int a, b; - cin >> a >> b; - adj[a].push_back(b); - } - - const int MAX_T = 1000; // Maximum number of days Bessie can travel - - // DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on - // day t - vector> dp(MAX_T + 1, vector(n + 1, -1)); - dp[0][1] = 0; // Base case: Start at city 1 on day 0 with 0 moonies - - int res = 0; - - for (int t = 0; t <= MAX_T; t++) { - for (int i = 1; i <= n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) { continue; } - - // Transition: Consider all roads from city i to its neighbors - for (int j : adj[i]) { - if (t + 1 <= MAX_T) { - // Update dp[t + 1][j] by considering a transition from an adjacent - // city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); - } - } - } - - // After processing all cities for day t, calculate the profit if Bessie returns - // to city 1 - res = max(res, dp[t][1] - c * t * t); - } - - cout << res << "\n"; - return 0; + freopen("time.in", "r", stdin); + freopen("time.out", "w", stdout); + + int n, m, c; + cin >> n >> m >> c; + + vector moonies(n); + for (int i = 0; i < n; i++) { + cin >> moonies[i]; + } + + vector> adj(n); + for (int i = 0; i < m; i++) { + int a, b; + cin >> a >> b; + a--, b--; // Convert 1-indexed input to 0-indexed + adj[a].push_back(b); + } + + const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel + + // dp[t][i]: max moonies at city i on day t + vector> dp(MAX_DAYS, vector(n, -1)); + dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + int res = 0; + + for (int t = 0; t < MAX_DAYS; t++) { + for (int i = 0; i < n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { + continue; + } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj[i]) { + if (t + 1 < MAX_DAYS) { + // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t); + } + + cout << res << "\n"; + return 0; } ``` @@ -90,62 +92,65 @@ int main() { import java.io.*; import java.util.*; -public class TimeIsMooney { - public static void main(String[] args) throws IOException { - BufferedReader br = new BufferedReader(new FileReader("time.in")); - PrintWriter pw = new PrintWriter(new FileWriter("time.out")); - - final int MAX_T = 1000; // Maximum number of days Bessie can travel - StringTokenizer st = new StringTokenizer(br.readLine()); - int n = Integer.parseInt(st.nextToken()); - int m = Integer.parseInt(st.nextToken()); - int c = Integer.parseInt(st.nextToken()); - - int[] moonies = new int[n + 1]; - st = new StringTokenizer(br.readLine()); - for (int i = 1; i <= n; i++) { moonies[i] = Integer.parseInt(st.nextToken()); } - - List> adj = new ArrayList<>(); - for (int i = 0; i <= n; i++) adj.add(new ArrayList<>()); - for (int i = 0; i < m; i++) { - st = new StringTokenizer(br.readLine()); - int u = Integer.parseInt(st.nextToken()); - int v = Integer.parseInt(st.nextToken()); - adj.get(u).add(v); - } - - // DP table: dp[t][i] represents the maximum moonies Bessie can have at city i - // on day t - long[][] dp = new long[MAX_T + 1][n + 1]; - for (int t = 0; t <= MAX_T; t++) { Arrays.fill(dp[t], -1); } - dp[0][1] = 0; // Base case: Start at city 1 on day 0 with 0 moonies - - long res = 0; - - for (int t = 0; t < MAX_T; t++) { - for (int i = 1; i <= n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) continue; - - // Transition: Consider all roads from city i to its neighbors - for (int neighbor : adj.get(i)) { - if (t + 1 <= MAX_T) { - // Update dp[t + 1][neighbor] by considering a transition from - // an adjacent city on the previous day - dp[t + 1][neighbor] = - Math.max(dp[t + 1][neighbor], dp[t][i] + moonies[neighbor]); - } - } - } - - // After processing all cities for day t, calculate the profit if Bessie - // returns to city 1 - res = Math.max(res, dp[t][1] - (long)c * t * t); - } - - pw.println(res); - pw.close(); - } +public class Main { + public 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"))); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + int[] moonies = new int[n]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < n; i++) { + moonies[i] = Integer.parseInt(st.nextToken()); + } + + List> adj = new ArrayList<>(); + for (int i = 0; i < n; i++) { + adj.add(new ArrayList<>()); + } + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()) - 1; + int b = Integer.parseInt(st.nextToken()) - 1; + adj.get(a).add(b); + } + + final int MAX_DAYS = 1000; // Maximum number of days Bessie can travel + + // dp[t][i]: max moonies at city i on day t + int[][] dp = new int[MAX_DAYS][n]; + for (int[] row : dp) Arrays.fill(row, -1); + dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + int res = 0; + + for (int t = 0; t < MAX_DAYS; t++) { + for (int i = 0; i < n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { + continue; + } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj.get(i)) { + if (t + 1 < MAX_DAYS) { + // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = Math.max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = Math.max(res, dp[t][0] - c * t * t); + } + + pw.println(res); + pw.close(); + } } ``` @@ -153,42 +158,46 @@ public class TimeIsMooney { ```py -MAX_DAYS = 1000 - -with open("time.in") as read: - n, m, c = map(int, read.readline().strip().split()) - moonies = [0] + list(map(int, read.readline().strip().split())) - - adj = [[] for _ in range(n + 1)] - for _ in range(m): - u, v = map(int, read.readline().strip().split()) - adj[u].append(v) - -# DP table: dp[t][i] represents the maximum moonies Bessie can have at city i on day t -dp = [[-1] * (n + 1) for _ in range(MAX_DAYS + 1)] -dp[0][1] = 0 # Base case: Start at city 1 on day 0 with 0 moonies - -res = 0 - -for t in range(MAX_DAYS): - for i in range(1, n + 1): - # Skip cities that are unreachable on day t - if dp[t][i] == -1: - continue - - # Transition: Consider all roads from city i to its neighbors - for neighbor in adj[i]: - if t + 1 <= MAX_DAYS: - # Update dp[t + 1][neighbor] by considering a transition from an adjacent city on the previous day - dp[t + 1][neighbor] = max( - dp[t + 1][neighbor], dp[t][i] + moonies[neighbor] - ) - - # After processing all cities for day t, calculate the profit if Bessie returns to city 1 - res = max(res, dp[t][1] - c * t * t) - -with open("time.out", "w") as write: - write.write(f"{res}\n") +def main(): + 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()) + a -= 1 # Convert 1-indexed input to 0-indexed + b -= 1 + adj[a].append(b) + + MAX_DAYS = 1000 # Maximum number of days Bessie can travel + + # dp[t][i]: max moonies at city i on day t + dp = [[-1] * n for _ in range(MAX_DAYS)] + dp[0][0] = 0 # Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + res = 0 + + for t in range(MAX_DAYS): + for i in range(n): + # Skip cities that are unreachable on day t + if dp[t][i] == -1: + continue + + # Transition: Consider all roads from city i to its neighbors + for j in adj[i]: + if t + 1 < MAX_DAYS: + # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) + + # Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t) + + with open("time.out", "w") as fout: + fout.write(f"{res}\n") + +if __name__ == "__main__": + main() ``` From 6fd2228154ee880cacfba7865d9dab562997029c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Dec 2024 20:40:38 +0000 Subject: [PATCH 04/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/gold/usaco-993.mdx | 286 +++++++++++++++++------------------ 1 file changed, 142 insertions(+), 144 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index d0a19eff46..5aeb426be4 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -33,55 +33,53 @@ approach ensures we consider all feasible trips and ignore unreachable states. using namespace std; int main() { - freopen("time.in", "r", stdin); - freopen("time.out", "w", stdout); - - int n, m, c; - cin >> n >> m >> c; - - vector moonies(n); - for (int i = 0; i < n; i++) { - cin >> moonies[i]; - } - - vector> adj(n); - for (int i = 0; i < m; i++) { - int a, b; - cin >> a >> b; - a--, b--; // Convert 1-indexed input to 0-indexed - adj[a].push_back(b); - } - - const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel - - // dp[t][i]: max moonies at city i on day t - vector> dp(MAX_DAYS, vector(n, -1)); - dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies - - int res = 0; - - for (int t = 0; t < MAX_DAYS; t++) { - for (int i = 0; i < n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) { - continue; - } - - // Transition: Consider all roads from city i to its neighbors - for (int j : adj[i]) { - if (t + 1 < MAX_DAYS) { - // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); - } - } - } - - // Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = max(res, dp[t][0] - c * t * t); - } - - cout << res << "\n"; - return 0; + freopen("time.in", "r", stdin); + freopen("time.out", "w", stdout); + + int n, m, c; + cin >> n >> m >> c; + + vector moonies(n); + for (int i = 0; i < n; i++) { cin >> moonies[i]; } + + vector> adj(n); + for (int i = 0; i < m; i++) { + int a, b; + cin >> a >> b; + a--, b--; // Convert 1-indexed input to 0-indexed + adj[a].push_back(b); + } + + const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel + + // dp[t][i]: max moonies at city i on day t + vector> dp(MAX_DAYS, vector(n, -1)); + dp[0][0] = + 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + int res = 0; + + for (int t = 0; t < MAX_DAYS; t++) { + for (int i = 0; i < n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { continue; } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj[i]) { + if (t + 1 < MAX_DAYS) { + // Update dp[t + 1][j] by considering a transition from an adjacent + // city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t); + } + + cout << res << "\n"; + return 0; } ``` @@ -93,64 +91,61 @@ import java.io.*; import java.util.*; public class Main { - public 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"))); - - StringTokenizer st = new StringTokenizer(br.readLine()); - int n = Integer.parseInt(st.nextToken()); - int m = Integer.parseInt(st.nextToken()); - int c = Integer.parseInt(st.nextToken()); - - int[] moonies = new int[n]; - st = new StringTokenizer(br.readLine()); - for (int i = 0; i < n; i++) { - moonies[i] = Integer.parseInt(st.nextToken()); - } - - List> adj = new ArrayList<>(); - for (int i = 0; i < n; i++) { - adj.add(new ArrayList<>()); - } - for (int i = 0; i < m; i++) { - st = new StringTokenizer(br.readLine()); - int a = Integer.parseInt(st.nextToken()) - 1; - int b = Integer.parseInt(st.nextToken()) - 1; - adj.get(a).add(b); - } - - final int MAX_DAYS = 1000; // Maximum number of days Bessie can travel - - // dp[t][i]: max moonies at city i on day t - int[][] dp = new int[MAX_DAYS][n]; - for (int[] row : dp) Arrays.fill(row, -1); - dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies - - int res = 0; - - for (int t = 0; t < MAX_DAYS; t++) { - for (int i = 0; i < n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) { - continue; - } - - // Transition: Consider all roads from city i to its neighbors - for (int j : adj.get(i)) { - if (t + 1 < MAX_DAYS) { - // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = Math.max(dp[t + 1][j], dp[t][i] + moonies[j]); - } - } - } - - // Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = Math.max(res, dp[t][0] - c * t * t); - } - - pw.println(res); - pw.close(); - } + public 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"))); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + int[] moonies = new int[n]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < n; i++) { moonies[i] = Integer.parseInt(st.nextToken()); } + + List> adj = new ArrayList<>(); + for (int i = 0; i < n; i++) { adj.add(new ArrayList<>()); } + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()) - 1; + int b = Integer.parseInt(st.nextToken()) - 1; + adj.get(a).add(b); + } + + final int MAX_DAYS = 1000; // Maximum number of days Bessie can travel + + // dp[t][i]: max moonies at city i on day t + int[][] dp = new int[MAX_DAYS][n]; + for (int[] row : dp) Arrays.fill(row, -1); + dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 + // moonies + + int res = 0; + + for (int t = 0; t < MAX_DAYS; t++) { + for (int i = 0; i < n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { continue; } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj.get(i)) { + if (t + 1 < MAX_DAYS) { + // Update dp[t + 1][j] by considering a transition from an + // adjacent city on the previous day + dp[t + 1][j] = Math.max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = Math.max(res, dp[t][0] - c * t * t); + } + + pw.println(res); + pw.close(); + } } ``` @@ -159,45 +154,48 @@ public class Main { ```py def main(): - 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()) - a -= 1 # Convert 1-indexed input to 0-indexed - b -= 1 - adj[a].append(b) - - MAX_DAYS = 1000 # Maximum number of days Bessie can travel - - # dp[t][i]: max moonies at city i on day t - dp = [[-1] * n for _ in range(MAX_DAYS)] - dp[0][0] = 0 # Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies - - res = 0 - - for t in range(MAX_DAYS): - for i in range(n): - # Skip cities that are unreachable on day t - if dp[t][i] == -1: - continue - - # Transition: Consider all roads from city i to its neighbors - for j in adj[i]: - if t + 1 < MAX_DAYS: - # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) - - # Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = max(res, dp[t][0] - c * t * t) - - with open("time.out", "w") as fout: - fout.write(f"{res}\n") + 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()) + a -= 1 # Convert 1-indexed input to 0-indexed + b -= 1 + adj[a].append(b) + + MAX_DAYS = 1000 # Maximum number of days Bessie can travel + + # dp[t][i]: max moonies at city i on day t + dp = [[-1] * n for _ in range(MAX_DAYS)] + dp[0][ + 0 + ] = 0 # Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + res = 0 + + for t in range(MAX_DAYS): + for i in range(n): + # Skip cities that are unreachable on day t + if dp[t][i] == -1: + continue + + # Transition: Consider all roads from city i to its neighbors + for j in adj[i]: + if t + 1 < MAX_DAYS: + # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) + + # Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t) + + with open("time.out", "w") as fout: + fout.write(f"{res}\n") + if __name__ == "__main__": - main() + main() ``` From 3649606a6b91733143e5830642d32d2a398d695a Mon Sep 17 00:00:00 2001 From: David Guo Date: Fri, 6 Dec 2024 23:05:03 -0800 Subject: [PATCH 05/14] Update usaco-993.mdx --- solutions/gold/usaco-993.mdx | 288 ++++++++++++++++++----------------- 1 file changed, 145 insertions(+), 143 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 5aeb426be4..524540862d 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -9,7 +9,7 @@ author: Nathan Gong, Ryan Chou, David Guo ## Explanation -The problem involves maximizing the profit Bessie can earn while traveling between cities and collecting moonies. In a trip, only two factors matter in evaluating it: the city Bessie is in and the day she is on. We need to explore all paths while efficiently keeping track of the best outcomes. A dynamic programming approach is well-suited for this, as it allows us to break the problem into smaller subproblems: tracking the maximum moonies Bessie can have at each city for every possible day. +The problem involves maximizing the profit Bessie can earn while traveling between cities and collecting moonies. We need to explore all paths while efficiently keeping track of the best outcomes. A dynamic programming approach is well-suited for this, as it allows us to break the problem into smaller subproblems: tracking the maximum moonies Bessie can have at each city for every possible day. We define $dp[t][i]$ as the maximum moonies Bessie can have at city $i$ on day $t$. Starting with $dp[0][0] = 0$, we iterate over each day up to $T_{\text{max}}$, the maximum number of days, and @@ -32,54 +32,56 @@ approach ensures we consider all feasible trips and ignore unreachable states. #include using namespace std; +const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel + int main() { - freopen("time.in", "r", stdin); - freopen("time.out", "w", stdout); - - int n, m, c; - cin >> n >> m >> c; - - vector moonies(n); - for (int i = 0; i < n; i++) { cin >> moonies[i]; } - - vector> adj(n); - for (int i = 0; i < m; i++) { - int a, b; - cin >> a >> b; - a--, b--; // Convert 1-indexed input to 0-indexed - adj[a].push_back(b); - } - - const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel - - // dp[t][i]: max moonies at city i on day t - vector> dp(MAX_DAYS, vector(n, -1)); - dp[0][0] = - 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies - - int res = 0; - - for (int t = 0; t < MAX_DAYS; t++) { - for (int i = 0; i < n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) { continue; } - - // Transition: Consider all roads from city i to its neighbors - for (int j : adj[i]) { - if (t + 1 < MAX_DAYS) { - // Update dp[t + 1][j] by considering a transition from an adjacent - // city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); - } - } - } - - // Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = max(res, dp[t][0] - c * t * t); - } - - cout << res << "\n"; - return 0; + freopen("time.in", "r", stdin); + freopen("time.out", "w", stdout); + + int n, m, c; + cin >> n >> m >> c; + + vector moonies(n); + for (int i = 0; i < n; i++) { + cin >> moonies[i]; + } + + vector> adj(n); + for (int i = 0; i < m; i++) { + int a, b; + cin >> a >> b; + a--, b--; // Convert 1-indexed input to 0-indexed + adj[a].push_back(b); + } + + // dp[t][i]: max moonies at city i on day t + vector> dp(MAX_DAYS, vector(n, -1)); + dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + int res = 0; + + for (int t = 0; t < MAX_DAYS; t++) { + for (int i = 0; i < n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { + continue; + } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj[i]) { + if (t + 1 < MAX_DAYS) { + // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t); + } + + cout << res << "\n"; + return 0; } ``` @@ -91,61 +93,64 @@ import java.io.*; import java.util.*; public class Main { - public 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"))); - - StringTokenizer st = new StringTokenizer(br.readLine()); - int n = Integer.parseInt(st.nextToken()); - int m = Integer.parseInt(st.nextToken()); - int c = Integer.parseInt(st.nextToken()); - - int[] moonies = new int[n]; - st = new StringTokenizer(br.readLine()); - for (int i = 0; i < n; i++) { moonies[i] = Integer.parseInt(st.nextToken()); } - - List> adj = new ArrayList<>(); - for (int i = 0; i < n; i++) { adj.add(new ArrayList<>()); } - for (int i = 0; i < m; i++) { - st = new StringTokenizer(br.readLine()); - int a = Integer.parseInt(st.nextToken()) - 1; - int b = Integer.parseInt(st.nextToken()) - 1; - adj.get(a).add(b); - } - - final int MAX_DAYS = 1000; // Maximum number of days Bessie can travel - - // dp[t][i]: max moonies at city i on day t - int[][] dp = new int[MAX_DAYS][n]; - for (int[] row : dp) Arrays.fill(row, -1); - dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 - // moonies - - int res = 0; - - for (int t = 0; t < MAX_DAYS; t++) { - for (int i = 0; i < n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) { continue; } - - // Transition: Consider all roads from city i to its neighbors - for (int j : adj.get(i)) { - if (t + 1 < MAX_DAYS) { - // Update dp[t + 1][j] by considering a transition from an - // adjacent city on the previous day - dp[t + 1][j] = Math.max(dp[t + 1][j], dp[t][i] + moonies[j]); - } - } - } - - // Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = Math.max(res, dp[t][0] - c * t * t); - } - - pw.println(res); - pw.close(); - } + static final int MAX_DAYS = 1000; // Maximum number of days Bessie can travel + + public 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"))); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + int[] moonies = new int[n]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < n; i++) { + moonies[i] = Integer.parseInt(st.nextToken()); + } + + List> adj = new ArrayList<>(); + for (int i = 0; i < n; i++) { + adj.add(new ArrayList<>()); + } + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()) - 1; + int b = Integer.parseInt(st.nextToken()) - 1; + adj.get(a).add(b); + } + + // dp[t][i]: max moonies at city i on day t + int[][] dp = new int[MAX_DAYS][n]; + for (int[] row : dp) Arrays.fill(row, -1); + dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + int res = 0; + + for (int t = 0; t < MAX_DAYS; t++) { + for (int i = 0; i < n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { + continue; + } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj.get(i)) { + if (t + 1 < MAX_DAYS) { + // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = Math.max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = Math.max(res, dp[t][0] - c * t * t); + } + + pw.println(res); + pw.close(); + } } ``` @@ -154,48 +159,45 @@ public class Main { ```py def main(): - 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()) - a -= 1 # Convert 1-indexed input to 0-indexed - b -= 1 - adj[a].append(b) - - MAX_DAYS = 1000 # Maximum number of days Bessie can travel - - # dp[t][i]: max moonies at city i on day t - dp = [[-1] * n for _ in range(MAX_DAYS)] - dp[0][ - 0 - ] = 0 # Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies - - res = 0 - - for t in range(MAX_DAYS): - for i in range(n): - # Skip cities that are unreachable on day t - if dp[t][i] == -1: - continue - - # Transition: Consider all roads from city i to its neighbors - for j in adj[i]: - if t + 1 < MAX_DAYS: - # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) - - # Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = max(res, dp[t][0] - c * t * t) - - with open("time.out", "w") as fout: - fout.write(f"{res}\n") - + 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()) + a -= 1 # Convert 1-indexed input to 0-indexed + b -= 1 + adj[a].append(b) + + MAX_DAYS = 1000 # Maximum number of days Bessie can travel + + # dp[t][i]: max moonies at city i on day t + dp = [[-1] * n for _ in range(MAX_DAYS)] + dp[0][0] = 0 # Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + res = 0 + + for t in range(MAX_DAYS): + for i in range(n): + # Skip cities that are unreachable on day t + if dp[t][i] == -1: + continue + + # Transition: Consider all roads from city i to its neighbors + for j in adj[i]: + if t + 1 < MAX_DAYS: + # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) + + # Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t) + + with open("time.out", "w") as fout: + fout.write(f"{res}\n") if __name__ == "__main__": - main() + main() ``` From 0ab7fcadd62e209ca54b06d92fae03b1a7eaedf3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Dec 2024 07:06:12 +0000 Subject: [PATCH 06/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/gold/usaco-993.mdx | 282 +++++++++++++++++------------------ 1 file changed, 140 insertions(+), 142 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 524540862d..078c6e5f9f 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -35,53 +35,51 @@ using namespace std; const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel int main() { - freopen("time.in", "r", stdin); - freopen("time.out", "w", stdout); - - int n, m, c; - cin >> n >> m >> c; - - vector moonies(n); - for (int i = 0; i < n; i++) { - cin >> moonies[i]; - } - - vector> adj(n); - for (int i = 0; i < m; i++) { - int a, b; - cin >> a >> b; - a--, b--; // Convert 1-indexed input to 0-indexed - adj[a].push_back(b); - } - - // dp[t][i]: max moonies at city i on day t - vector> dp(MAX_DAYS, vector(n, -1)); - dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies - - int res = 0; - - for (int t = 0; t < MAX_DAYS; t++) { - for (int i = 0; i < n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) { - continue; - } - - // Transition: Consider all roads from city i to its neighbors - for (int j : adj[i]) { - if (t + 1 < MAX_DAYS) { - // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); - } - } - } - - // Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = max(res, dp[t][0] - c * t * t); - } - - cout << res << "\n"; - return 0; + freopen("time.in", "r", stdin); + freopen("time.out", "w", stdout); + + int n, m, c; + cin >> n >> m >> c; + + vector moonies(n); + for (int i = 0; i < n; i++) { cin >> moonies[i]; } + + vector> adj(n); + for (int i = 0; i < m; i++) { + int a, b; + cin >> a >> b; + a--, b--; // Convert 1-indexed input to 0-indexed + adj[a].push_back(b); + } + + // dp[t][i]: max moonies at city i on day t + vector> dp(MAX_DAYS, vector(n, -1)); + dp[0][0] = + 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + int res = 0; + + for (int t = 0; t < MAX_DAYS; t++) { + for (int i = 0; i < n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { continue; } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj[i]) { + if (t + 1 < MAX_DAYS) { + // Update dp[t + 1][j] by considering a transition from an adjacent + // city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t); + } + + cout << res << "\n"; + return 0; } ``` @@ -93,64 +91,61 @@ import java.io.*; import java.util.*; public class Main { - static final int MAX_DAYS = 1000; // Maximum number of days Bessie can travel - - public 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"))); - - StringTokenizer st = new StringTokenizer(br.readLine()); - int n = Integer.parseInt(st.nextToken()); - int m = Integer.parseInt(st.nextToken()); - int c = Integer.parseInt(st.nextToken()); - - int[] moonies = new int[n]; - st = new StringTokenizer(br.readLine()); - for (int i = 0; i < n; i++) { - moonies[i] = Integer.parseInt(st.nextToken()); - } - - List> adj = new ArrayList<>(); - for (int i = 0; i < n; i++) { - adj.add(new ArrayList<>()); - } - for (int i = 0; i < m; i++) { - st = new StringTokenizer(br.readLine()); - int a = Integer.parseInt(st.nextToken()) - 1; - int b = Integer.parseInt(st.nextToken()) - 1; - adj.get(a).add(b); - } - - // dp[t][i]: max moonies at city i on day t - int[][] dp = new int[MAX_DAYS][n]; - for (int[] row : dp) Arrays.fill(row, -1); - dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies - - int res = 0; - - for (int t = 0; t < MAX_DAYS; t++) { - for (int i = 0; i < n; i++) { - // Skip cities that are unreachable on day t - if (dp[t][i] == -1) { - continue; - } - - // Transition: Consider all roads from city i to its neighbors - for (int j : adj.get(i)) { - if (t + 1 < MAX_DAYS) { - // Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = Math.max(dp[t + 1][j], dp[t][i] + moonies[j]); - } - } - } - - // Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = Math.max(res, dp[t][0] - c * t * t); - } - - pw.println(res); - pw.close(); - } + static final int MAX_DAYS = 1000; // Maximum number of days Bessie can travel + + public 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"))); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + int[] moonies = new int[n]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < n; i++) { moonies[i] = Integer.parseInt(st.nextToken()); } + + List> adj = new ArrayList<>(); + for (int i = 0; i < n; i++) { adj.add(new ArrayList<>()); } + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()) - 1; + int b = Integer.parseInt(st.nextToken()) - 1; + adj.get(a).add(b); + } + + // dp[t][i]: max moonies at city i on day t + int[][] dp = new int[MAX_DAYS][n]; + for (int[] row : dp) Arrays.fill(row, -1); + dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 + // moonies + + int res = 0; + + for (int t = 0; t < MAX_DAYS; t++) { + for (int i = 0; i < n; i++) { + // Skip cities that are unreachable on day t + if (dp[t][i] == -1) { continue; } + + // Transition: Consider all roads from city i to its neighbors + for (int j : adj.get(i)) { + if (t + 1 < MAX_DAYS) { + // Update dp[t + 1][j] by considering a transition from an + // adjacent city on the previous day + dp[t + 1][j] = Math.max(dp[t + 1][j], dp[t][i] + moonies[j]); + } + } + } + + // Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = Math.max(res, dp[t][0] - c * t * t); + } + + pw.println(res); + pw.close(); + } } ``` @@ -159,45 +154,48 @@ public class Main { ```py def main(): - 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()) - a -= 1 # Convert 1-indexed input to 0-indexed - b -= 1 - adj[a].append(b) - - MAX_DAYS = 1000 # Maximum number of days Bessie can travel - - # dp[t][i]: max moonies at city i on day t - dp = [[-1] * n for _ in range(MAX_DAYS)] - dp[0][0] = 0 # Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies - - res = 0 - - for t in range(MAX_DAYS): - for i in range(n): - # Skip cities that are unreachable on day t - if dp[t][i] == -1: - continue - - # Transition: Consider all roads from city i to its neighbors - for j in adj[i]: - if t + 1 < MAX_DAYS: - # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) - - # Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = max(res, dp[t][0] - c * t * t) - - with open("time.out", "w") as fout: - fout.write(f"{res}\n") + 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()) + a -= 1 # Convert 1-indexed input to 0-indexed + b -= 1 + adj[a].append(b) + + MAX_DAYS = 1000 # Maximum number of days Bessie can travel + + # dp[t][i]: max moonies at city i on day t + dp = [[-1] * n for _ in range(MAX_DAYS)] + dp[0][ + 0 + ] = 0 # Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + + res = 0 + + for t in range(MAX_DAYS): + for i in range(n): + # Skip cities that are unreachable on day t + if dp[t][i] == -1: + continue + + # Transition: Consider all roads from city i to its neighbors + for j in adj[i]: + if t + 1 < MAX_DAYS: + # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) + + # Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t) + + with open("time.out", "w") as fout: + fout.write(f"{res}\n") + if __name__ == "__main__": - main() + main() ``` From 1d1032881207739b64fb9d784683036447e6c9ec Mon Sep 17 00:00:00 2001 From: David Guo Date: Mon, 9 Dec 2024 22:46:28 -0800 Subject: [PATCH 07/14] Update usaco-993.mdx --- solutions/gold/usaco-993.mdx | 77 ++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 078c6e5f9f..3f2161836a 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -21,6 +21,8 @@ we calculate the profit as $dp[t][0] - C \cdot t^2$, representing the total moonies minus the travel cost for $t$ days, and track the maximum profit. This approach ensures we consider all feasible trips and ignore unreachable states. +Note that the maximum amount of money Bessie makes is $1000t - t^2$, in the case where she makes $1000$ mooney per city and the cost is $1$ mooney. Her earnings become negative when $t > 1000$, so we only have to check her movement across the cities for at most $1000$ days. + ## Implementation **Time Complexity:** $\mathcal{O}(T_{\text{max}} \cdot (N + M))$ @@ -35,8 +37,8 @@ using namespace std; const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel int main() { - freopen("time.in", "r", stdin); - freopen("time.out", "w", stdout); + ifstream cin("time.in"); + ofstream cout("time.out"); int n, m, c; cin >> n >> m >> c; @@ -54,8 +56,8 @@ int main() { // dp[t][i]: max moonies at city i on day t vector> dp(MAX_DAYS, vector(n, -1)); - dp[0][0] = - 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + dp[0][0] = 0; int res = 0; @@ -79,7 +81,6 @@ int main() { } cout << res << "\n"; - return 0; } ``` @@ -119,8 +120,8 @@ public class Main { // dp[t][i]: max moonies at city i on day t int[][] dp = new int[MAX_DAYS][n]; for (int[] row : dp) Arrays.fill(row, -1); - dp[0][0] = 0; // Base case: Start at city 0 (originally city 1) on day 0 with 0 - // moonies + // Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + dp[0][0] = 0; int res = 0; @@ -153,49 +154,41 @@ public class Main { ```py -def main(): - 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()) - a -= 1 # Convert 1-indexed input to 0-indexed - b -= 1 - adj[a].append(b) - - MAX_DAYS = 1000 # Maximum number of days Bessie can travel +with open("time.in", "r") as fin: + n, m, c = map(int, fin.readline().split()) + moonies = list(map(int, fin.readline().split())) - # dp[t][i]: max moonies at city i on day t - dp = [[-1] * n for _ in range(MAX_DAYS)] - dp[0][ - 0 - ] = 0 # Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies + 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-indexed - res = 0 +MAX_DAYS = 1000 # Maximum number of days Bessie can travel - for t in range(MAX_DAYS): - for i in range(n): - # Skip cities that are unreachable on day t - if dp[t][i] == -1: - continue +# dp[t][i]: max moonies at city i on day t +dp = [[-1] * n for _ in range(MAX_DAYS)] +# Base case: Start at city 0 (originally city 1) on day 0 with 0 moonies +dp[0][0] = 0 - # Transition: Consider all roads from city i to its neighbors - for j in adj[i]: - if t + 1 < MAX_DAYS: - # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day - dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) +res = 0 - # Calculate profit if Bessie returns to city 0 (originally city 1) on day t - res = max(res, dp[t][0] - c * t * t) +for t in range(MAX_DAYS): + for i in range(n): + # Skip cities that are unreachable on day t + if dp[t][i] == -1: + continue - with open("time.out", "w") as fout: - fout.write(f"{res}\n") + # Transition: Consider all roads from city i to its neighbors + for j in adj[i]: + if t + 1 < MAX_DAYS: + # Update dp[t + 1][j] by considering a transition from an adjacent city on the previous day + dp[t + 1][j] = max(dp[t + 1][j], dp[t][i] + moonies[j]) + # Calculate profit if Bessie returns to city 0 (originally city 1) on day t + res = max(res, dp[t][0] - c * t * t) -if __name__ == "__main__": - main() +with open("time.out", "w") as fout: + fout.write(f"{res}\n") ``` From 8f26f17051f5797e9d09e59784e0695a09420a48 Mon Sep 17 00:00:00 2001 From: David Guo Date: Wed, 11 Dec 2024 14:09:28 -0800 Subject: [PATCH 08/14] Update usaco-993.mdx --- solutions/gold/usaco-993.mdx | 2 -- 1 file changed, 2 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 3f2161836a..7481d439be 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -9,8 +9,6 @@ author: Nathan Gong, Ryan Chou, David Guo ## Explanation -The problem involves maximizing the profit Bessie can earn while traveling between cities and collecting moonies. We need to explore all paths while efficiently keeping track of the best outcomes. A dynamic programming approach is well-suited for this, as it allows us to break the problem into smaller subproblems: tracking the maximum moonies Bessie can have at each city for every possible day. - We define $dp[t][i]$ as the maximum moonies Bessie can have at city $i$ on day $t$. Starting with $dp[0][0] = 0$, we iterate over each day up to $T_{\text{max}}$, the maximum number of days, and over each city, updating $dp[t + 1][j]$ for all cities $j$ reachable from city From 00267ad78fdad7fed0a6062dd1d389f46bd2c6d9 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:03:44 -0800 Subject: [PATCH 09/14] Update usaco-993.mdx --- solutions/gold/usaco-993.mdx | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 7481d439be..f9df320695 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -9,17 +9,21 @@ author: Nathan Gong, Ryan Chou, David Guo ## Explanation -We define $dp[t][i]$ as the maximum moonies Bessie can have at city $i$ on day $t$. Starting with $dp[0][0] = 0$, we -iterate over each day up to $T_{\text{max}}$, the maximum number of days, and -over each city, updating $dp[t + 1][j]$ for all cities $j$ reachable from city -$i$ via a directed road. We compare the current value of $dp[t + 1][j]$ with the -value of coming from an adjacent city on the previous day, -$dp[t][i] + \text{moonies}[j]$, and take the maximum. After processing each day, -we calculate the profit as $dp[t][0] - C \cdot t^2$, representing the total -moonies minus the travel cost for $t$ days, and track the maximum profit. This -approach ensures we consider all feasible trips and ignore unreachable states. +We define $dp[t][i]$ as the maximum moonies Bessie can have at city $i$ on day $t$. +Starting with $dp[0][0] = 0$, we iterate over each day up to $T_{\text{max}}$, +the maximum number of days, and over each city, updating $dp[t + 1][j]$ +for all cities $j$ reachable from city $i$ via a directed road. -Note that the maximum amount of money Bessie makes is $1000t - t^2$, in the case where she makes $1000$ mooney per city and the cost is $1$ mooney. Her earnings become negative when $t > 1000$, so we only have to check her movement across the cities for at most $1000$ days. +We compare the current value of $dp[t + 1][j]$ with the +value of coming from an adjacent city on the previous day, +$dp[t][i] + \text{moonies}[j]$, and take the maximum. +After processing each day, we calculate the profit as $dp[t][0] - C \cdot t^2$, +representing the total moonies minus the travel cost for $t$ days, +and track the maximum profit. + +Note that the maximum amount of money Bessie makes is $1000t - t^2$, +in the case where she makes $1000$ mooney per city and the cost is $1$ mooney. +Her earnings become negative when $t > 1000$, so we only have to check her movement across the cities for at most $1000$ days. ## Implementation From 980f5a33943acce7249f178847a12ce0c12199fb Mon Sep 17 00:00:00 2001 From: David Guo Date: Wed, 11 Dec 2024 15:58:17 -0800 Subject: [PATCH 10/14] Update usaco-993.mdx --- solutions/gold/usaco-993.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index f9df320695..b92a519346 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -40,7 +40,6 @@ const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel int main() { ifstream cin("time.in"); - ofstream cout("time.out"); int n, m, c; cin >> n >> m >> c; @@ -82,7 +81,7 @@ int main() { res = max(res, dp[t][0] - c * t * t); } - cout << res << "\n"; + ofstream("time.out") << res << "\n"; } ``` From cf0697801d0a37bd639045786d30864fde8f6bb7 Mon Sep 17 00:00:00 2001 From: David Guo Date: Wed, 11 Dec 2024 19:24:59 -0800 Subject: [PATCH 11/14] Update solutions/gold/usaco-993.mdx Co-authored-by: Bing-Dong Liu --- solutions/gold/usaco-993.mdx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index b92a519346..9db458f73b 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -9,15 +9,15 @@ author: Nathan Gong, Ryan Chou, David Guo ## Explanation -We define $dp[t][i]$ as the maximum moonies Bessie can have at city $i$ on day $t$. -Starting with $dp[0][0] = 0$, we iterate over each day up to $T_{\text{max}}$, -the maximum number of days, and over each city, updating $dp[t + 1][j]$ +We define $\texttt{dp}[t][i]$ as the maximum moonies Bessie can have at city $i$ on day $t$. +Starting with $\texttt{dp}[0][0] = 0$, we iterate over each day up to $T_{\text{max}}$, +the maximum number of days, and over each city, updating $\texttt{dp}[t + 1][j]$ for all cities $j$ reachable from city $i$ via a directed road. -We compare the current value of $dp[t + 1][j]$ with the +We compare the current value of $\texttt{dp}[t + 1][j]$ with the value of coming from an adjacent city on the previous day, -$dp[t][i] + \text{moonies}[j]$, and take the maximum. -After processing each day, we calculate the profit as $dp[t][0] - C \cdot t^2$, +$\texttt{dp}[t][i] + \text{moonies}[j]$, and take the maximum. +After processing each day, we calculate the profit as $\texttt{dp}[t][0] - C \cdot t^2$, representing the total moonies minus the travel cost for $t$ days, and track the maximum profit. From 38ed490604b3903e44773f233f5128ed685a6786 Mon Sep 17 00:00:00 2001 From: David Guo Date: Wed, 11 Dec 2024 19:30:58 -0800 Subject: [PATCH 12/14] Update usaco-993.mdx --- solutions/gold/usaco-993.mdx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 9db458f73b..a640227730 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -21,8 +21,9 @@ After processing each day, we calculate the profit as $\texttt{dp}[t][0] - C \cd representing the total moonies minus the travel cost for $t$ days, and track the maximum profit. -Note that the maximum amount of money Bessie makes is $1000t - t^2$, -in the case where she makes $1000$ mooney per city and the cost is $1$ mooney. +Note that the maximum amount of money Bessie makes is $1000t - t^2$ +in the case where she makes $1000$ moonies per city and the cost is $1$ mooney. This is also because the edge weights +$m_i$ are bounded by $1000$ moonies. Her earnings become negative when $t > 1000$, so we only have to check her movement across the cities for at most $1000$ days. ## Implementation From a3d987d0d49b6df292d1b5ed5019c9bbca042f4c Mon Sep 17 00:00:00 2001 From: David Guo Date: Wed, 11 Dec 2024 20:10:18 -0800 Subject: [PATCH 13/14] Update usaco-993.mdx --- solutions/gold/usaco-993.mdx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index a640227730..16065052f5 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -40,18 +40,18 @@ using namespace std; const int MAX_DAYS = 1000; // Maximum number of days Bessie can travel int main() { - ifstream cin("time.in"); + ifstream read("time.in"); int n, m, c; - cin >> n >> m >> c; + read >> n >> m >> c; vector moonies(n); - for (int i = 0; i < n; i++) { cin >> moonies[i]; } + for (int i = 0; i < n; i++) { read >> moonies[i]; } vector> adj(n); for (int i = 0; i < m; i++) { int a, b; - cin >> a >> b; + read >> a >> b; a--, b--; // Convert 1-indexed input to 0-indexed adj[a].push_back(b); } From e56cff904b3d4999aae6af1829366dbda1b027b5 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Wed, 11 Dec 2024 20:49:24 -0800 Subject: [PATCH 14/14] Update usaco-993.mdx --- solutions/gold/usaco-993.mdx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/solutions/gold/usaco-993.mdx b/solutions/gold/usaco-993.mdx index 16065052f5..ebc45fdfcb 100644 --- a/solutions/gold/usaco-993.mdx +++ b/solutions/gold/usaco-993.mdx @@ -22,9 +22,10 @@ representing the total moonies minus the travel cost for $t$ days, and track the maximum profit. Note that the maximum amount of money Bessie makes is $1000t - t^2$ -in the case where she makes $1000$ moonies per city and the cost is $1$ mooney. This is also because the edge weights -$m_i$ are bounded by $1000$ moonies. -Her earnings become negative when $t > 1000$, so we only have to check her movement across the cities for at most $1000$ days. +in the case where she makes $1000$ moonies per city and the cost is $1$ mooney. +This is also because the edge weights $m_i$ are bounded by $1000$ moonies. +Her earnings become negative when $t > 1000$, so we only have to check her +movement across the cities for at most $1000$ days. ## Implementation