1 / 11

The Aftercow

The Aftercow. Replacing math with cow since 1993. PotW Solution (credits to Jimmy). class Edge implements Comparable<Edge> { int toNode , dist; public Edge(int a, int b ) { toNode = a; dist = b; } public int compareTo (Edge e ) { return dist - e.dist ; } }

abiba
Download Presentation

The Aftercow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Aftercow Replacing math with cow since 1993

  2. PotW Solution (credits to Jimmy) class Edge implements Comparable<Edge> {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<Edge>[] 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 • 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 • “Right-angle path” = path that changes direction at most once, moves only up/down/left/right • Runtime of O(N3) works • Use this graph w/ Dijkstra’s algorithm • 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) • 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 • 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: • 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 • 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 • 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 • Each cow actively moves in at most one photograph • Given the contents of each photograph, reconstruct the original intended order of the cows • 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 • 3 or more: A is before B in original ordering • Else: B is before A • Use this as comparator and sort the list

  11. PotW • No Problem of the Week this week • 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!

More Related