Download presentation
Presentation is loading. Please wait.
Published byMelanie Chase Modified over 9 years ago
1
Computer Science Club It is easier to change the specification to fit the program than vice versa.
2
Last Week’s Problem Denise’s fabulousness Set of items, each with a weight and fabulousness Max fabulousness restricted to a certain weight
3
Solution public class Item { private int weight; private int fabulousness; public Item( int weight, int fabulousness ) { this.weight = weight; this.fabulousness = fabulousness; } public int getWeight() { return weight; } public int getFabulousness() { return fabulousness; } }
4
Solution (cont.) Variables: static int[][] fabKnapsackMemo = new int[1000][9012];
5
Solution (cont.) public static int fabulousKnapsackCorrect( Item[] items, int itemNum, int maxWeight ) { // check bounds if( itemNum = items.length ) return 0; // haven't already calculated this value if( fabKnapsackMemo[itemNum][maxWeight] == 0 ) { Item item = items[itemNum]; int itemWeight = item.getWeight(); int itemValue = item.getFabulousness();
6
Solution (cont.) // 0 items or 0 weight will both result in a // max fabulousness of 0 fabKnapsackMemo[0][maxWeight] = 0; fabKnapsackMemo[itemNum][0] = 0; // can't add this item because it's too heavy if( itemWeight > maxWeight ) // just as good as best fabulousness with one less item fabKnapsackMemo[itemNum][maxWeight] = fabulousKnapsackCorrect( items, itemNum - 1, maxWeight );
7
Solution (cont.) else fabKnapsackMemo[itemNum][maxWeight] = Math.max( // best fabulousness with one less item fabulousKnapsackCorrect( items, itemNum - 1, maxWeight ), // maximum fabulousness of enough weight to accommodate this item plus the fabulousness of it fabulousKnapsackCorrect( items, itemNum - 1, maxWeight - itemWeight ) + itemValue ); } // max fabulousness return fabKnapsackMemo[itemNum][maxWeight]; }
8
USACO Today is the last day to take the contest http://contest.usaco.org If you want to try it out or practice, take bronze – Bronze doesn’t count for anything Everybody should at least try silver/gold Read the problem and directions – Read the tips for Java on the contest page – The timer starts when you click the problems – Read the input conditions – Read how they want you to process input/output
9
USACO (cont.) ~3 hours (2 for qualification rounds) ~3-4 problems (2 for qualification rounds) Header: /* ID: youridhere LANG: C/C++/JAVA PROG: progname */ Scan: “progname.in” and write: “progname.out” Good luck!
10
Problem of the Week Cancelled due to USACO Algorithm presentation instead
11
Dijkstra’s Algorithm Used to find the shortest path between a starting position and destination. Given a graph with specified distances of the directional paths between nodes: Task: Find the shortest path from Node a (initial node) to Node f (destination). For example, A-C-D-F has a distance of 3 + 10 + 2 = 15. The path A-C-E-F has a distance of 3 + 3 + 5 = 11. Is there a path even SHORTER than that? Can you be sure your path is the shortest possible?
12
3 2 Dijkstra’s (cont.) The Steps Step 1: Each node has a distance value. (a = 0, others = ∞) Step 2: Two node sets: visited and unvisited (init: none visited) Step 3: a = curNode Step 4: (recursive step!) - Update unvisited neighbors of curNode with new shortest dist - move curNode to visited set - new curNode = smallest unvisited node
13
3 2 Dijkstra’s (cont.) Finishing Up When curNode = destination, shortest path = value of final node. Try to trace the algorithm and find the shortest path and minimal distance. Consider… Why don’t we need to re-check visited nodes? Why can’t there be a shorter path to a visited node?
14
Wrap Up No membership dues Remember to sign in Take USACO Thanks for coming
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.