Download presentation

Presentation is loading. Please wait.

Published byLaura Hilton Modified over 3 years ago

1
The Aftercow Replacing math with cow since 1993

2
PotW Solution PotW Solution (credits to Jimmy) class Edge implements Comparable { int toNode, dist; public Edge(int a, int b) { toNode = a; dist = b; } public int compareTo(Edge e) { return dist - e.dist; } } Used for both storing the graph and locating closest points within the PriorityQueue in Dijkstra's

3
PotW Solution (cont.) for (int i = 0; i < e; i++) { int node1 = input.nextInt() - 1; int node2 = input.nextInt() - 1; int dist = input.nextInt(); graphThere[node1].add(new Edge(node2, dist)); graphBack[node2].add(new Edge(node1, dist)); } int[] distThere = dijkstra(graphThere, v, e); int[] distBack = dijkstra(graphBack, v, e); int count = 0; for (int i = 0; i < v; i++) { if (distThere[i] + distBack[i] <= money) { count++; } } System.out.println(count);

4
PotW Solution (cont.) int[] dijkstra(ArrayList [] graph, int v, int e) {... while (!pq.isEmpty()) { Edge e1 = pq.poll(); if (visited[e1.toNode]) continue; visited[e1.toNode] = true; for (int i = 0; i < graph[e1.toNode].size(); i++) { Edge e2 = graph[e1.toNode].get(i); int nextDist = dist[e1.toNode] + e2.dist; if (dist[e2.toNode] > nextDist) { pq.offer(new Edge(e2.toNode, nextDist)); dist[e2.toNode] = nextDist; } return dist; }

5
January Silver #1: Delivery Route N (1 <= N <= 100) farms located at different (integer) positions in the x-y plane Farmer John wants to visit each farm in sequential order and then return to farm 1 (1, 2, 3, …, N, 1) Takes one minute to make a step either north, south, east, or west You can only cross each farm once Determine the minimum amount of time it’ll take to complete entire delivery route (or -1 if no feasible delivery route is possible)

6
Delivery Route “Minimum time” suggests Dijkstra’s algorithm o Constructing the graph is the hard part Create 5 nodes for each farm, one for the farm itself and the 4 adjacent coordinates Add an edge between every pair of nodes such that it’s possible to go from (x1, y1) to (x2, y2) using a right-angle path w/o intersecting other farms o “Right-angle path” = path that changes direction at most once, moves only up/down/left/right o Runtime of O(N 3 ) works Use this graph w/ Dijkstra’s algorithm o Shortest path between farms is achievable using only these nodes and right-angle paths

7
January Gold #3: Bovine Alliance The cows in each of N farms (1 <= N <= 100,000) were instructed to build a trail to exactly one other farm (N trails total) o However, only M of these trails have been built Farms are arguing over which farms have built trails Calculate the number of ways the M trails could have been built, modulo 1,000,000,007.

8
Bovine Alliance Imagine it as a set of vertices and edges, with edges pointing to the vertex that built the edge o Count number of assignments such that each edge points to some vertex, and each vertex has at most one edge pointing to it Solve the problem for each connected component, multiply the number of possiblities for each together If component has n vertices, there are two different cases for the number of edges: o n-1: component is a tree, number of assignments is n You can leave any of the n cows, and there is one solution for each such possibility o n: component is a cycle, number of assignments is 2 The cycle can point in two directions Use Depth-First Search to find and categorize each connected component o Count node and edges as you DFS

9
December Gold/Silver #1: Cow Photography Farmer John wants to take a photograph of up to 20,000 cows standing in a specific order Every time he’s about to take a picture a group of zero or more cows moves to a set of new positions This happens for 5 photographs o Each cow actively moves in at most one photograph Given the contents of each photograph, reconstruct the original intended order of the cows o This sounds familiar…

10
Cow Photography The solution basically involves sorting the cows Consider two cows from the set, A and B Neither A nor B moved in at least 3 of the 5 photographs Compare the number of times A comes before B o 3 or more: A is before B in original ordering o Else: B is before A Use this as comparator and sort the list

11
PotW No Problem of the Week this week o Practice on your own! :D Submissions for the tiebreaker “Cow Trails” problem over the December break have been graded and ranked Will be announced next meeting, along with prizes for the top scorers of first semester!

Similar presentations

OK

Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.

Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google