From 3b349cf2bc21d257e1592c1030c042f3ee434b47 Mon Sep 17 00:00:00 2001 From: raymo Date: Tue, 27 Aug 2019 02:15:58 -0700 Subject: [PATCH 01/27] ???? --- cowjump.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/cowjump.java b/cowjump.java index cbf5e89..2e7c5e0 100644 --- a/cowjump.java +++ b/cowjump.java @@ -31,8 +31,8 @@ public static void testIntersections() { assert linesCompare(new LineSegement(new Point(0,0), new Point(2,3)),new LineSegement(new Point(0,3),new Point(9,1))) == -1; System.out.println("All Tests OK!"); } - - public static boolean sweepCheck(LineSegement s,Point[][] input) { + + public static boolean sweepCheck(LineSegement s,Point[][] input) { for(int i = 0; i < input.length; i ++) { System.out.println("Checking line "+i); if(input[i][0] == null && input[i][1] == null) { @@ -68,8 +68,8 @@ public static boolean sweepCheck(LineSegement s,Point[][] input) { System.out.println("No Intersection!"); } } - - } + + } return false; } public static void main(String[] args) throws IOException{ @@ -103,8 +103,8 @@ public static void main(String[] args) throws IOException{ if(status) { break; } - - input[i][0] = a; + + input[i][0] = a; input[i][1] = b; /*for(int j = 0; j < i; j ++) { if(Point.intersection(input[j][0], input[j][1], input[i][0], input[i][1])) { @@ -198,11 +198,17 @@ public static void main(String[] args) throws IOException{ pw.println(output+1); pw.close(); System.exit(0); + + } +<<<<<<< HEAD <<<<<<< HEAD } } +======= +} +>>>>>>> parent of 84bd17b... Merge branch 'master' of https://github.com/javaarchive/Java class Point{ double x,y; ======= @@ -344,7 +350,7 @@ public int compareTo(Point arg0) { } } - class LineSegement { +class LineSegement { Point a,b; public LineSegement(Point a,Point b) { if(a.x > b.x) { @@ -385,4 +391,4 @@ public Point atX_(double x) { public String toString() { return this.a.toString() + " -- "+this.b.toString(); } -} \ No newline at end of file +} \ No newline at end of file From bd338c1486491deeacbef18d46e983bfbd91846a Mon Sep 17 00:00:00 2001 From: raymo Date: Tue, 27 Aug 2019 02:19:01 -0700 Subject: [PATCH 02/27] ???? --- dining.java | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 dining.java diff --git a/dining.java b/dining.java new file mode 100644 index 0000000..c8a141d --- /dev/null +++ b/dining.java @@ -0,0 +1,115 @@ +import java.io.*; +import java.util.*; +public class dining { + public static final int NO_PARENT = -1; // Constant for no parent + public static void main(String[] args) throws IOException { + BufferedReader f = new BufferedReader(new FileReader("dining.in")); + StringTokenizer st = new StringTokenizer(f.readLine()); + int N,M,K; + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + final int numOfCows = N - 1; + List> graph = new ArrayList<>(N); + for(int i = 0; i < N; i ++) { + graph.add(new ArrayList<>()); + } + Map cost = new HashMap<>(); + for(int i =0 ; i < M; i ++) { + st = new StringTokenizer(f.readLine()); + int start = Integer.parseInt(st.nextToken()); + int end = Integer.parseInt(st.nextToken()); + int edgeCost = Integer.parseInt(st.nextToken()); + graph.get(start-1).add(end-1); + graph.get(end-1).add(start-1); + cost.put(new Pair(start-1,end-1), edgeCost); + } + Map costWithHaybales = new HashMap<>(cost); + System.out.println("Cost without haybales: "+cost); + System.out.println("Graph as an edgelist: "+graph); + //int[] taste = new int[N]; + Pair key; + //cost.get(new Pair(0,1)); + for(int i = 0; i < K; i ++) { + st = new StringTokenizer(f.readLine()); + int index = Integer.parseInt(st.nextToken()) - 1; + int value = Integer.parseInt(st.nextToken()); + //costWithHaybales.get(new Pair(0,1)); + for(int node: graph.get(index)) { + key = new Pair(index,node);//assert key.equals(new Pair(node,index)); + //System.out.println("Map: "+costWithHaybales); + //System.out.println(key+ " "+costWithHaybales.get(key)); + costWithHaybales.put(key, + costWithHaybales.get(key) - value); + } + //taste[index] = value; + } + System.out.println("Cost with haybales: "+costWithHaybales); + f.close(); + int[] empty = new int[M]; + Arrays.fill(empty, Integer.MAX_VALUE); + int[] distTo = new int[M]; + for(int i = 0; i < numOfCows; i ++) { + distTo = Arrays.copyOf(empty, M); + } + } + +} + +// Order does not matter pair +class Pair{ + int x,y; + public Pair(int x,int y) { + this.x =x; + this.y =y; + } + @Override + public String toString() { + return "("+this.x+","+this.y+")"; + } + @Override + public boolean equals(Object obj) { + if(obj instanceof Pair) { + Pair p = (Pair) obj; + if((p.x == this.x && p.y == this.y) || (p.x == this.y && this.x == p.y)) { + return true; + } + }else { + return false; + } + return false; + } + @Override + public int hashCode(){ + return (Integer.hashCode(this.x) + 3) * (Integer.hashCode(this.y) + 3); + } +} +class Edge{ + int x; + int y; + int weight; + public Edge(int x,int y,int weight) { + this.x = x; + this.y =y; + this.weight = weight; + } + public String toString() { + return "("+this.x+"--"+this.y+" = "+this.weight+")"; + } + @Override + public boolean equals(Object obj) { + if(obj instanceof Edge) { + Edge other = (Edge) obj; + if((this.x == other.x && this.y == other.y) || (this.x == other.y && this.y == other.x)) { + if(this.weight == other.weight) { + return true; + }else { + return false; + } + } + return false; + }else { + return false; + } + } +} \ No newline at end of file From 33c1f3b698cf11a09149d39029a415314054014d Mon Sep 17 00:00:00 2001 From: raymo Date: Sun, 1 Sep 2019 11:17:17 -0700 Subject: [PATCH 03/27] Update dining.java --- dining.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/dining.java b/dining.java index c8a141d..6e0c97b 100644 --- a/dining.java +++ b/dining.java @@ -27,7 +27,7 @@ public static void main(String[] args) throws IOException { Map costWithHaybales = new HashMap<>(cost); System.out.println("Cost without haybales: "+cost); System.out.println("Graph as an edgelist: "+graph); - //int[] taste = new int[N]; + int[] taste = new int[N]; Pair key; //cost.get(new Pair(0,1)); for(int i = 0; i < K; i ++) { @@ -42,18 +42,17 @@ public static void main(String[] args) throws IOException { costWithHaybales.put(key, costWithHaybales.get(key) - value); } - //taste[index] = value; + taste[index] = value; } System.out.println("Cost with haybales: "+costWithHaybales); f.close(); int[] empty = new int[M]; Arrays.fill(empty, Integer.MAX_VALUE); int[] distTo = new int[M]; - for(int i = 0; i < numOfCows; i ++) { - distTo = Arrays.copyOf(empty, M); - } + distTo= Arrays.copyOf(empty, M); + + // begin dijkstra from barn } - } // Order does not matter pair From fa38ff8ea037e97d0cf3231be9fee05f2afd950e Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sun, 1 Sep 2019 11:50:26 -0700 Subject: [PATCH 04/27] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 662651b..cb10713 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,4 @@ Find release dates at https://javaarchive.github.io/Java/. Moved to https://gith # Important 2019 update, goes into effect 12/14/2018 The usacotools java library will be offically moved to it's own repositroy. Link is https://github.com/javaarchive/java-tools/tree/master # Maintaining -Sorry everyone! I only upload my usaco files for now +I only upload my usaco files for now...check out JavaProjects for other stuff From 0b3abab82ea6d3fb9a8563080f48dc04b36748d1 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sun, 1 Sep 2019 20:14:43 -0700 Subject: [PATCH 05/27] Update dining.java --- dining.java | 63 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/dining.java b/dining.java index 6e0c97b..b854da8 100644 --- a/dining.java +++ b/dining.java @@ -1,6 +1,8 @@ import java.io.*; import java.util.*; public class dining { + public static int[] distTo; + public static int[] distOrig; public static final int NO_PARENT = -1; // Constant for no parent public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new FileReader("dining.in")); @@ -46,15 +48,66 @@ public static void main(String[] args) throws IOException { } System.out.println("Cost with haybales: "+costWithHaybales); f.close(); - int[] empty = new int[M]; + int[] empty = new int[N]; Arrays.fill(empty, Integer.MAX_VALUE); - int[] distTo = new int[M]; - distTo= Arrays.copyOf(empty, M); + //distTo = new int[M]; + distTo= Arrays.copyOf(empty, N); + distOrig= Arrays.copyOf(empty, N); + Set visited = new HashSet<>(); + PriorityQueue nextNodes = new PriorityQueue<>(new Comparator() { + @Override + public int compare(Integer arg0, Integer arg1) { + return Integer.compare(dining.distTo[arg1], dining.distTo[arg0]); + //return 0; + } + }); + nextNodes.add(N-1); + // begin dijkstra from barn with haybales + while(visited.size() < N) { + System.out.println(visited.size()); + int u = nextNodes.remove(); // Get next node + System.out.println(u); + visited.add(u); + List adj = graph.get(u); + for(int node:adj) { + if(!visited.contains(node)) { + int edgeDist = costWithHaybales.get(new Pair(u,node)); + int totalDist = distTo[u] + edgeDist; + if (totalDist < distTo[node]) + distTo[node] = totalDist; + nextNodes.add(node); + } + } + } + PriorityQueue nextNodesOrig = new PriorityQueue<>(new Comparator() { + @Override + public int compare(Integer arg0, Integer arg1) { + return Integer.compare(dining.distOrig[arg1], dining.distOrig[arg0]); + //return 0; + } + }); + visited.clear(); + nextNodesOrig.add(N-1); + // begin dijkstra from barn without haybales + while(visited.size() < N) { + int u = nextNodesOrig.remove(); // Get next node + visited.add(u); + List adj = graph.get(u); + for(int node:adj) { + if(!visited.contains(node)) { + int edgeDist = cost.get(new Pair(u,node)); + int totalDist = distOrig[u] + edgeDist; + if (totalDist < distOrig[node]) + distOrig[node] = totalDist; + nextNodesOrig.add(node); + } + } + } + System.out.println("Output :" + Arrays.toString(distOrig)); + System.out.println("Output(haybales):" + Arrays.toString(distTo)); - // begin dijkstra from barn } } - // Order does not matter pair class Pair{ int x,y; From c8fcfac1137545b40cfe85b2718c8af13f27cf9d Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sun, 1 Sep 2019 21:18:11 -0700 Subject: [PATCH 06/27] Add files via upload --- dining.java | 385 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 219 insertions(+), 166 deletions(-) diff --git a/dining.java b/dining.java index b854da8..806633b 100644 --- a/dining.java +++ b/dining.java @@ -1,167 +1,220 @@ -import java.io.*; -import java.util.*; -public class dining { - public static int[] distTo; - public static int[] distOrig; - public static final int NO_PARENT = -1; // Constant for no parent - public static void main(String[] args) throws IOException { - BufferedReader f = new BufferedReader(new FileReader("dining.in")); - StringTokenizer st = new StringTokenizer(f.readLine()); - int N,M,K; - N = Integer.parseInt(st.nextToken()); - M = Integer.parseInt(st.nextToken()); - K = Integer.parseInt(st.nextToken()); - final int numOfCows = N - 1; - List> graph = new ArrayList<>(N); - for(int i = 0; i < N; i ++) { - graph.add(new ArrayList<>()); - } - Map cost = new HashMap<>(); - for(int i =0 ; i < M; i ++) { - st = new StringTokenizer(f.readLine()); - int start = Integer.parseInt(st.nextToken()); - int end = Integer.parseInt(st.nextToken()); - int edgeCost = Integer.parseInt(st.nextToken()); - graph.get(start-1).add(end-1); - graph.get(end-1).add(start-1); - cost.put(new Pair(start-1,end-1), edgeCost); - } - Map costWithHaybales = new HashMap<>(cost); - System.out.println("Cost without haybales: "+cost); - System.out.println("Graph as an edgelist: "+graph); - int[] taste = new int[N]; - Pair key; - //cost.get(new Pair(0,1)); - for(int i = 0; i < K; i ++) { - st = new StringTokenizer(f.readLine()); - int index = Integer.parseInt(st.nextToken()) - 1; - int value = Integer.parseInt(st.nextToken()); - //costWithHaybales.get(new Pair(0,1)); - for(int node: graph.get(index)) { - key = new Pair(index,node);//assert key.equals(new Pair(node,index)); - //System.out.println("Map: "+costWithHaybales); - //System.out.println(key+ " "+costWithHaybales.get(key)); - costWithHaybales.put(key, - costWithHaybales.get(key) - value); - } - taste[index] = value; - } - System.out.println("Cost with haybales: "+costWithHaybales); - f.close(); - int[] empty = new int[N]; - Arrays.fill(empty, Integer.MAX_VALUE); - //distTo = new int[M]; - distTo= Arrays.copyOf(empty, N); - distOrig= Arrays.copyOf(empty, N); - Set visited = new HashSet<>(); - PriorityQueue nextNodes = new PriorityQueue<>(new Comparator() { - @Override - public int compare(Integer arg0, Integer arg1) { - return Integer.compare(dining.distTo[arg1], dining.distTo[arg0]); - //return 0; - } - }); - nextNodes.add(N-1); - // begin dijkstra from barn with haybales - while(visited.size() < N) { - System.out.println(visited.size()); - int u = nextNodes.remove(); // Get next node - System.out.println(u); - visited.add(u); - List adj = graph.get(u); - for(int node:adj) { - if(!visited.contains(node)) { - int edgeDist = costWithHaybales.get(new Pair(u,node)); - int totalDist = distTo[u] + edgeDist; - if (totalDist < distTo[node]) - distTo[node] = totalDist; - nextNodes.add(node); - } - } - } - PriorityQueue nextNodesOrig = new PriorityQueue<>(new Comparator() { - @Override - public int compare(Integer arg0, Integer arg1) { - return Integer.compare(dining.distOrig[arg1], dining.distOrig[arg0]); - //return 0; - } - }); - visited.clear(); - nextNodesOrig.add(N-1); - // begin dijkstra from barn without haybales - while(visited.size() < N) { - int u = nextNodesOrig.remove(); // Get next node - visited.add(u); - List adj = graph.get(u); - for(int node:adj) { - if(!visited.contains(node)) { - int edgeDist = cost.get(new Pair(u,node)); - int totalDist = distOrig[u] + edgeDist; - if (totalDist < distOrig[node]) - distOrig[node] = totalDist; - nextNodesOrig.add(node); - } - } - } - System.out.println("Output :" + Arrays.toString(distOrig)); - System.out.println("Output(haybales):" + Arrays.toString(distTo)); - - } -} -// Order does not matter pair -class Pair{ - int x,y; - public Pair(int x,int y) { - this.x =x; - this.y =y; - } - @Override - public String toString() { - return "("+this.x+","+this.y+")"; - } - @Override - public boolean equals(Object obj) { - if(obj instanceof Pair) { - Pair p = (Pair) obj; - if((p.x == this.x && p.y == this.y) || (p.x == this.y && this.x == p.y)) { - return true; - } - }else { - return false; - } - return false; - } - @Override - public int hashCode(){ - return (Integer.hashCode(this.x) + 3) * (Integer.hashCode(this.y) + 3); - } -} -class Edge{ - int x; - int y; - int weight; - public Edge(int x,int y,int weight) { - this.x = x; - this.y =y; - this.weight = weight; - } - public String toString() { - return "("+this.x+"--"+this.y+" = "+this.weight+")"; - } - @Override - public boolean equals(Object obj) { - if(obj instanceof Edge) { - Edge other = (Edge) obj; - if((this.x == other.x && this.y == other.y) || (this.x == other.y && this.y == other.x)) { - if(this.weight == other.weight) { - return true; - }else { - return false; - } - } - return false; - }else { - return false; - } - } +import java.io.*; +import java.util.*; +public class dining { + public static int[] distTo; + public static int[] distOrig; + public static final int NO_PARENT = -1; // Constant for no parent + public static void main(String[] args) throws IOException { + BufferedReader f = new BufferedReader(new FileReader("dining.in")); + StringTokenizer st = new StringTokenizer(f.readLine()); + int N,M,K; + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + final int numOfCows = N - 1; + List> graph = new ArrayList<>(N); + for(int i = 0; i < N; i ++) { + graph.add(new ArrayList<>()); + } + Map cost = new HashMap<>(); + for(int i =0 ; i < M; i ++) { + st = new StringTokenizer(f.readLine()); + int start = Integer.parseInt(st.nextToken()); + int end = Integer.parseInt(st.nextToken()); + int edgeCost = Integer.parseInt(st.nextToken()); + graph.get(start-1).add(end-1); + graph.get(end-1).add(start-1); + cost.put(new Pair(start-1,end-1), edgeCost); + } + Map costWithHaybales = new HashMap<>(cost); + //System.out.println("Cost without haybales: "+cost); + //System.out.println("Graph as an edgelist: "+graph); + int[] taste = new int[N]; + Pair key; + List bales = new ArrayList<>(); + //Map haybales = new HashMap<>(); + //cost.get(new Pair(0,1)); + for(int i = 0; i < K; i ++) { + st = new StringTokenizer(f.readLine()); + int index = Integer.parseInt(st.nextToken()) - 1; + bales.add(index); + int value = Integer.parseInt(st.nextToken()); + + //costWithHaybales.get(new Pair(0,1)); + for(int node: graph.get(index)) { + key = new Pair(index,node);//assert key.equals(new Pair(node,index)); + //System.out.println("Map: "+costWithHaybales); + //System.out.println(key+ " "+costWithHaybales.get(key)); + //costWithHaybales.put(key, + // costWithHaybales.get(key) - value); + } + taste[index] = value; + } + //System.out.println("Cost with haybales: "+costWithHaybales); + f.close(); + int[] empty = new int[N]; + Arrays.fill(empty, Integer.MAX_VALUE); + empty[N-1] = 0; + //distTo = new int[M]; + //distTo= Arrays.copyOf(empty, N); + distOrig= Arrays.copyOf(empty, N); + Set visited = new HashSet<>(); + PriorityQueue nextNodes = new PriorityQueue<>(new Comparator() { + @Override + public int compare(Integer arg0, Integer arg1) { + return Integer.compare(dining.distTo[arg1], dining.distTo[arg0]); + //return 0; + } + }); + /* + nextNodes.add(N-1); + // begin dijkstra from barn with haybales + while(visited.size() < N) { + System.out.println(visited.size()); + int u = nextNodes.remove(); // Get next node + System.out.println(u); + visited.add(u); + List adj = graph.get(u); + for(int node:adj) { + if(!visited.contains(node)) { + int edgeDist = costWithHaybales.get(new Pair(u,node)); + int totalDist = distTo[u] + edgeDist; + if (totalDist < distTo[node]) + distTo[node] = totalDist; + nextNodes.add(node); + } + } + } + */ + PriorityQueue nextNodesOrig = new PriorityQueue<>(new Comparator() { + @Override + public int compare(Integer arg0, Integer arg1) { + return Integer.compare(dining.distOrig[arg1], dining.distOrig[arg0]); + //return 0; + } + }); + //visited.clear(); + + nextNodesOrig.add(N-1); + // begin dijkstra from barn without haybales + while(visited.size() < N) { + int u = nextNodesOrig.remove(); // Get next node + visited.add(u); + List adj = graph.get(u); + for(int node:adj) { + if(!visited.contains(node)) { + int edgeDist = cost.get(new Pair(u,node)); + int totalDist = distOrig[u] + edgeDist; + if (totalDist < distOrig[node]) + distOrig[node] = totalDist; + nextNodesOrig.add(node); + } + } + } + //System.out.println("Output :" + Arrays.toString(distOrig)); + + //for(int bale: graph.get(N-1)) { + // graph.get(bale).remove((Object) (N-1)); + //} + + //graph.get(N-1).clear(); + + for(int i = 0 ; i < K; i++) { + int target = bales.get(i); + //for(int bale:graph.get(target)) { + // graph.get(bale).remove(target); + //} + //graph.get(target).clear(); + graph.get(target).add(N-1); + graph.get(N-1).add(target); + costWithHaybales.put(new Pair(target,N-1), distOrig[target] - taste[i]); + } + visited.clear(); + distTo= Arrays.copyOf(empty, N); + nextNodes.add(N-1); + // begin dijkstra from barn with haybales + while(visited.size() < N) { + //System.out.println(visited.size()); + int u = nextNodes.remove(); // Get next node + //System.out.println(u); + visited.add(u); + List adj = graph.get(u); + for(int node:adj) { + if(!visited.contains(node)) { + int edgeDist = costWithHaybales.get(new Pair(u,node)); + int totalDist = distTo[u] + edgeDist; + if (totalDist < distTo[node]) + distTo[node] = totalDist; + nextNodes.add(node); + } + } + } + //System.out.println("Output(haybales):" + Arrays.toString(distTo)); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dining.out"))); + for(int i =0 ; i < numOfCows; i ++) { + if(distTo[i] <= distOrig[i]) { + pw.println(1); + }else { + pw.println(0); + } + } + pw.close(); + } +} +// Order does not matter pair +class Pair{ + int x,y; + public Pair(int x,int y) { + this.x =x; + this.y =y; + } + @Override + public String toString() { + return "("+this.x+","+this.y+")"; + } + @Override + public boolean equals(Object obj) { + if(obj instanceof Pair) { + Pair p = (Pair) obj; + if((p.x == this.x && p.y == this.y) || (p.x == this.y && this.x == p.y)) { + return true; + } + }else { + return false; + } + return false; + } + @Override + public int hashCode(){ + return (Integer.hashCode(this.x) + 3) * (Integer.hashCode(this.y) + 3); + } +} +class Edge{ + int x; + int y; + int weight; + public Edge(int x,int y,int weight) { + this.x = x; + this.y =y; + this.weight = weight; + } + public String toString() { + return "("+this.x+"--"+this.y+" = "+this.weight+")"; + } + @Override + public boolean equals(Object obj) { + if(obj instanceof Edge) { + Edge other = (Edge) obj; + if((this.x == other.x && this.y == other.y) || (this.x == other.y && this.y == other.x)) { + if(this.weight == other.weight) { + return true; + }else { + return false; + } + } + return false; + }else { + return false; + } + } } \ No newline at end of file From 6e2021b1042571300293a9b1b39ec802b3d0649b Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 09:39:57 -0700 Subject: [PATCH 07/27] Update dining.java --- dining.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dining.java b/dining.java index 806633b..1407f9d 100644 --- a/dining.java +++ b/dining.java @@ -30,7 +30,7 @@ public static void main(String[] args) throws IOException { //System.out.println("Cost without haybales: "+cost); //System.out.println("Graph as an edgelist: "+graph); int[] taste = new int[N]; - Pair key; + //Pair key; List bales = new ArrayList<>(); //Map haybales = new HashMap<>(); //cost.get(new Pair(0,1)); @@ -39,15 +39,16 @@ public static void main(String[] args) throws IOException { int index = Integer.parseInt(st.nextToken()) - 1; bales.add(index); int value = Integer.parseInt(st.nextToken()); - + /* //costWithHaybales.get(new Pair(0,1)); for(int node: graph.get(index)) { - key = new Pair(index,node);//assert key.equals(new Pair(node,index)); + Pair key = new Pair(index,node);//assert key.equals(new Pair(node,index)); //System.out.println("Map: "+costWithHaybales); //System.out.println(key+ " "+costWithHaybales.get(key)); //costWithHaybales.put(key, // costWithHaybales.get(key) - value); } + */ taste[index] = value; } //System.out.println("Cost with haybales: "+costWithHaybales); @@ -62,7 +63,7 @@ public static void main(String[] args) throws IOException { PriorityQueue nextNodes = new PriorityQueue<>(new Comparator() { @Override public int compare(Integer arg0, Integer arg1) { - return Integer.compare(dining.distTo[arg1], dining.distTo[arg0]); + return Integer.compare(dining.distTo[arg0], dining.distTo[arg1]); //return 0; } }); @@ -89,7 +90,7 @@ public int compare(Integer arg0, Integer arg1) { PriorityQueue nextNodesOrig = new PriorityQueue<>(new Comparator() { @Override public int compare(Integer arg0, Integer arg1) { - return Integer.compare(dining.distOrig[arg1], dining.distOrig[arg0]); + return Integer.compare(dining.distOrig[arg0], dining.distOrig[arg1]); //return 0; } }); From 3142e2bdc7f31baf11038cbbc29a629c22c5bd4a Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 09:44:00 -0700 Subject: [PATCH 08/27] Update dining.java --- dining.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dining.java b/dining.java index 1407f9d..563c156 100644 --- a/dining.java +++ b/dining.java @@ -153,7 +153,7 @@ public int compare(Integer arg0, Integer arg1) { //System.out.println("Output(haybales):" + Arrays.toString(distTo)); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dining.out"))); for(int i =0 ; i < numOfCows; i ++) { - if(distTo[i] <= distOrig[i]) { + if(distTo[i] >= distOrig[i]) { pw.println(1); }else { pw.println(0); From 3bcb87877d14e372fbae8ab3f5156a3b74e926d4 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 09:46:25 -0700 Subject: [PATCH 09/27] Update dining.java --- dining.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dining.java b/dining.java index 563c156..efc6d81 100644 --- a/dining.java +++ b/dining.java @@ -180,16 +180,16 @@ public boolean equals(Object obj) { if((p.x == this.x && p.y == this.y) || (p.x == this.y && this.x == p.y)) { return true; } - }else { - return false; } return false; } @Override public int hashCode(){ - return (Integer.hashCode(this.x) + 3) * (Integer.hashCode(this.y) + 3); + return Integer.hashCode(this.x) * Integer.hashCode(this.y); } } +// No longer needed +/* class Edge{ int x; int y; @@ -218,4 +218,4 @@ public boolean equals(Object obj) { return false; } } -} \ No newline at end of file +}*/ \ No newline at end of file From c52d26e8ce855023d1956d4f3e3c28c4ad204d69 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 10:19:08 -0700 Subject: [PATCH 10/27] Update dining.java --- dining.java | 101 +++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 61 deletions(-) diff --git a/dining.java b/dining.java index efc6d81..a72659c 100644 --- a/dining.java +++ b/dining.java @@ -1,11 +1,38 @@ import java.io.*; import java.util.*; public class dining { - public static int[] distTo; - public static int[] distOrig; - public static final int NO_PARENT = -1; // Constant for no parent + + public static int[] dijkstra(Map cost, List> graph, final int N) { + int[] dist = new int[N]; + Set visited = new HashSet<>(); + PriorityQueue nodeQueue = new PriorityQueue<>(new Comparator() { + @Override + public int compare(Integer arg0, Integer arg1) { + return Integer.compare(dist[arg0], dist[arg1]); + //return 0; + } + }); + Arrays.fill(dist, Integer.MAX_VALUE); + dist[N-1] = 0; // Starting value + nodeQueue.add(N-1); + while(visited.size() < N) { + int u = nodeQueue.remove(); // Get next node + visited.add(u); + List adj = graph.get(u); + for(int node:adj) { + if(!visited.contains(node)) { + int edgeDist = cost.get(new Pair(u,node)); + int totalDist = dist[u] + edgeDist; + if (totalDist < dist[node]) + dist[node] = totalDist; + nodeQueue.add(node); + } + } + } + return dist; + } public static void main(String[] args) throws IOException { - BufferedReader f = new BufferedReader(new FileReader("dining.in")); + BufferedReader f = new BufferedReader(new FileReader("2.in")); StringTokenizer st = new StringTokenizer(f.readLine()); int N,M,K; N = Integer.parseInt(st.nextToken()); @@ -28,7 +55,7 @@ public static void main(String[] args) throws IOException { } Map costWithHaybales = new HashMap<>(cost); //System.out.println("Cost without haybales: "+cost); - //System.out.println("Graph as an edgelist: "+graph); + System.out.println("Graph as an edgelist: "+graph); int[] taste = new int[N]; //Pair key; List bales = new ArrayList<>(); @@ -58,15 +85,7 @@ public static void main(String[] args) throws IOException { empty[N-1] = 0; //distTo = new int[M]; //distTo= Arrays.copyOf(empty, N); - distOrig= Arrays.copyOf(empty, N); - Set visited = new HashSet<>(); - PriorityQueue nextNodes = new PriorityQueue<>(new Comparator() { - @Override - public int compare(Integer arg0, Integer arg1) { - return Integer.compare(dining.distTo[arg0], dining.distTo[arg1]); - //return 0; - } - }); + int[] distOrig,distTo; /* nextNodes.add(N-1); // begin dijkstra from barn with haybales @@ -87,32 +106,8 @@ public int compare(Integer arg0, Integer arg1) { } } */ - PriorityQueue nextNodesOrig = new PriorityQueue<>(new Comparator() { - @Override - public int compare(Integer arg0, Integer arg1) { - return Integer.compare(dining.distOrig[arg0], dining.distOrig[arg1]); - //return 0; - } - }); - //visited.clear(); - nextNodesOrig.add(N-1); - // begin dijkstra from barn without haybales - while(visited.size() < N) { - int u = nextNodesOrig.remove(); // Get next node - visited.add(u); - List adj = graph.get(u); - for(int node:adj) { - if(!visited.contains(node)) { - int edgeDist = cost.get(new Pair(u,node)); - int totalDist = distOrig[u] + edgeDist; - if (totalDist < distOrig[node]) - distOrig[node] = totalDist; - nextNodesOrig.add(node); - } - } - } - //System.out.println("Output :" + Arrays.toString(distOrig)); + distOrig = dijkstra(cost, graph, N); //for(int bale: graph.get(N-1)) { // graph.get(bale).remove((Object) (N-1)); @@ -130,30 +125,14 @@ public int compare(Integer arg0, Integer arg1) { graph.get(N-1).add(target); costWithHaybales.put(new Pair(target,N-1), distOrig[target] - taste[i]); } - visited.clear(); - distTo= Arrays.copyOf(empty, N); - nextNodes.add(N-1); - // begin dijkstra from barn with haybales - while(visited.size() < N) { - //System.out.println(visited.size()); - int u = nextNodes.remove(); // Get next node - //System.out.println(u); - visited.add(u); - List adj = graph.get(u); - for(int node:adj) { - if(!visited.contains(node)) { - int edgeDist = costWithHaybales.get(new Pair(u,node)); - int totalDist = distTo[u] + edgeDist; - if (totalDist < distTo[node]) - distTo[node] = totalDist; - nextNodes.add(node); - } - } - } - //System.out.println("Output(haybales):" + Arrays.toString(distTo)); + System.out.println("Cost : "+cost); + System.out.println("Cost with haybales: "+costWithHaybales); + distTo = dijkstra(costWithHaybales, graph, N); + System.out.println("Output :" + Arrays.toString(distOrig)); + System.out.println("Output(haybales):" + Arrays.toString(distTo)); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dining.out"))); for(int i =0 ; i < numOfCows; i ++) { - if(distTo[i] >= distOrig[i]) { + if(distTo[i] <= distOrig[i]) { pw.println(1); }else { pw.println(0); From 948577b089cef16255cda0711a7e33fc243c7d4d Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 10:25:13 -0700 Subject: [PATCH 11/27] removed lots of old and unnesscary code --- dining.java | 81 +---------------------------------------------------- 1 file changed, 1 insertion(+), 80 deletions(-) diff --git a/dining.java b/dining.java index a72659c..3c36b25 100644 --- a/dining.java +++ b/dining.java @@ -54,77 +54,29 @@ public static void main(String[] args) throws IOException { cost.put(new Pair(start-1,end-1), edgeCost); } Map costWithHaybales = new HashMap<>(cost); - //System.out.println("Cost without haybales: "+cost); System.out.println("Graph as an edgelist: "+graph); int[] taste = new int[N]; - //Pair key; List bales = new ArrayList<>(); - //Map haybales = new HashMap<>(); - //cost.get(new Pair(0,1)); for(int i = 0; i < K; i ++) { st = new StringTokenizer(f.readLine()); int index = Integer.parseInt(st.nextToken()) - 1; bales.add(index); int value = Integer.parseInt(st.nextToken()); - /* - //costWithHaybales.get(new Pair(0,1)); - for(int node: graph.get(index)) { - Pair key = new Pair(index,node);//assert key.equals(new Pair(node,index)); - //System.out.println("Map: "+costWithHaybales); - //System.out.println(key+ " "+costWithHaybales.get(key)); - //costWithHaybales.put(key, - // costWithHaybales.get(key) - value); - } - */ taste[index] = value; } - //System.out.println("Cost with haybales: "+costWithHaybales); f.close(); int[] empty = new int[N]; Arrays.fill(empty, Integer.MAX_VALUE); empty[N-1] = 0; - //distTo = new int[M]; - //distTo= Arrays.copyOf(empty, N); int[] distOrig,distTo; - /* - nextNodes.add(N-1); - // begin dijkstra from barn with haybales - while(visited.size() < N) { - System.out.println(visited.size()); - int u = nextNodes.remove(); // Get next node - System.out.println(u); - visited.add(u); - List adj = graph.get(u); - for(int node:adj) { - if(!visited.contains(node)) { - int edgeDist = costWithHaybales.get(new Pair(u,node)); - int totalDist = distTo[u] + edgeDist; - if (totalDist < distTo[node]) - distTo[node] = totalDist; - nextNodes.add(node); - } - } - } - */ - distOrig = dijkstra(cost, graph, N); - - //for(int bale: graph.get(N-1)) { - // graph.get(bale).remove((Object) (N-1)); - //} - - //graph.get(N-1).clear(); - for(int i = 0 ; i < K; i++) { int target = bales.get(i); - //for(int bale:graph.get(target)) { - // graph.get(bale).remove(target); - //} - //graph.get(target).clear(); graph.get(target).add(N-1); graph.get(N-1).add(target); costWithHaybales.put(new Pair(target,N-1), distOrig[target] - taste[i]); } + System.out.println("Modified New Graph : "+graph); System.out.println("Cost : "+cost); System.out.println("Cost with haybales: "+costWithHaybales); distTo = dijkstra(costWithHaybales, graph, N); @@ -167,34 +119,3 @@ public int hashCode(){ return Integer.hashCode(this.x) * Integer.hashCode(this.y); } } -// No longer needed -/* -class Edge{ - int x; - int y; - int weight; - public Edge(int x,int y,int weight) { - this.x = x; - this.y =y; - this.weight = weight; - } - public String toString() { - return "("+this.x+"--"+this.y+" = "+this.weight+")"; - } - @Override - public boolean equals(Object obj) { - if(obj instanceof Edge) { - Edge other = (Edge) obj; - if((this.x == other.x && this.y == other.y) || (this.x == other.y && this.y == other.x)) { - if(this.weight == other.weight) { - return true; - }else { - return false; - } - } - return false; - }else { - return false; - } - } -}*/ \ No newline at end of file From 2fe0c3e175dd53f53f492d57506f080bc4827dc4 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 12:19:31 -0700 Subject: [PATCH 12/27] Working but with high memory usage and slow speed Attempting to use Map to represent edges Keeping weight data strucuture --- dining.java | 60 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/dining.java b/dining.java index 3c36b25..39c2cb8 100644 --- a/dining.java +++ b/dining.java @@ -1,8 +1,7 @@ import java.io.*; import java.util.*; public class dining { - - public static int[] dijkstra(Map cost, List> graph, final int N) { + public static int[] dijkstra(Map cost, Map> graph, final int N, int source) { int[] dist = new int[N]; Set visited = new HashSet<>(); PriorityQueue nodeQueue = new PriorityQueue<>(new Comparator() { @@ -32,56 +31,69 @@ public int compare(Integer arg0, Integer arg1) { return dist; } public static void main(String[] args) throws IOException { - BufferedReader f = new BufferedReader(new FileReader("2.in")); + BufferedReader f = new BufferedReader(new FileReader("3.in")); StringTokenizer st = new StringTokenizer(f.readLine()); int N,M,K; N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); K = Integer.parseInt(st.nextToken()); final int numOfCows = N - 1; - List> graph = new ArrayList<>(N); - for(int i = 0; i < N; i ++) { - graph.add(new ArrayList<>()); - } + Map> graph = new HashMap<>(M+1); + //for(int i = 0; i < N+1; i ++) { + // graph.add(new ArrayList<>()); + //} Map cost = new HashMap<>(); for(int i =0 ; i < M; i ++) { st = new StringTokenizer(f.readLine()); int start = Integer.parseInt(st.nextToken()); int end = Integer.parseInt(st.nextToken()); int edgeCost = Integer.parseInt(st.nextToken()); + if(!graph.containsKey(start-1)) { + graph.put(start-1, new ArrayList<>()); + } + if(!graph.containsKey(end-1)) { + graph.put(end-1, new ArrayList<>()); + } graph.get(start-1).add(end-1); graph.get(end-1).add(start-1); cost.put(new Pair(start-1,end-1), edgeCost); } + //System.out.println("Cost : "+cost); Map costWithHaybales = new HashMap<>(cost); - System.out.println("Graph as an edgelist: "+graph); - int[] taste = new int[N]; + //System.out.println("Graph as an edgelist: "+graph); + int[] taste = new int[K]; List bales = new ArrayList<>(); for(int i = 0; i < K; i ++) { st = new StringTokenizer(f.readLine()); int index = Integer.parseInt(st.nextToken()) - 1; bales.add(index); int value = Integer.parseInt(st.nextToken()); - taste[index] = value; + taste[i] = value; } f.close(); - int[] empty = new int[N]; - Arrays.fill(empty, Integer.MAX_VALUE); - empty[N-1] = 0; + //int[] empty = new int[N]; + //Arrays.fill(empty, Integer.MAX_VALUE); + //empty[N-1] = 0; int[] distOrig,distTo; - distOrig = dijkstra(cost, graph, N); + distOrig = dijkstra(cost, graph, N, N - 1); + //for(int bale: graph.get(N-1)) { + // graph.get(bale).remove((Object) (N-1)); + //} + //graph.get(N-1).clear(); for(int i = 0 ; i < K; i++) { int target = bales.get(i); - graph.get(target).add(N-1); - graph.get(N-1).add(target); - costWithHaybales.put(new Pair(target,N-1), distOrig[target] - taste[i]); + graph.get(target).add(N); + graph.get(N).add(target); + //System.out.println("Dist: "+distOrig[target]+" Taste: "+taste[i]); + //System.out.println(target+" -- "+(N-1) + " connected with weight "+(distOrig[target] - taste[i])); + costWithHaybales.put(new Pair(target,N), distOrig[target] - taste[i]); } - System.out.println("Modified New Graph : "+graph); - System.out.println("Cost : "+cost); - System.out.println("Cost with haybales: "+costWithHaybales); - distTo = dijkstra(costWithHaybales, graph, N); - System.out.println("Output :" + Arrays.toString(distOrig)); - System.out.println("Output(haybales):" + Arrays.toString(distTo)); + //System.out.println("Modified New Graph : "+graph); + //System.out.println("Cost : "+cost); + //System.out.println("Cost with haybales: "+costWithHaybales); + distTo = dijkstra(costWithHaybales, graph, N+1, N); + //System.out.println("Output :" + Arrays.toString(distOrig)); + //System.out.println("Output(haybales):" + Arrays.toString(distTo)); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dining.out"))); for(int i =0 ; i < numOfCows; i ++) { if(distTo[i] <= distOrig[i]) { @@ -118,4 +130,4 @@ public boolean equals(Object obj) { public int hashCode(){ return Integer.hashCode(this.x) * Integer.hashCode(this.y); } -} +} \ No newline at end of file From e19a2ab7441bb8109b96245d45a8d37d8598d09b Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 12:21:04 -0700 Subject: [PATCH 13/27] revert to adjancey list original method --- dining.java | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/dining.java b/dining.java index 39c2cb8..90436c6 100644 --- a/dining.java +++ b/dining.java @@ -1,7 +1,7 @@ import java.io.*; import java.util.*; public class dining { - public static int[] dijkstra(Map cost, Map> graph, final int N, int source) { + public static int[] dijkstra(Map cost, List> graph, final int N, int source) { int[] dist = new int[N]; Set visited = new HashSet<>(); PriorityQueue nodeQueue = new PriorityQueue<>(new Comparator() { @@ -38,29 +38,23 @@ public static void main(String[] args) throws IOException { M = Integer.parseInt(st.nextToken()); K = Integer.parseInt(st.nextToken()); final int numOfCows = N - 1; - Map> graph = new HashMap<>(M+1); - //for(int i = 0; i < N+1; i ++) { - // graph.add(new ArrayList<>()); - //} + List> graph = new ArrayList<>(N+1); + for(int i = 0; i < N+1; i ++) { + graph.add(new ArrayList<>()); + } Map cost = new HashMap<>(); for(int i =0 ; i < M; i ++) { st = new StringTokenizer(f.readLine()); int start = Integer.parseInt(st.nextToken()); int end = Integer.parseInt(st.nextToken()); int edgeCost = Integer.parseInt(st.nextToken()); - if(!graph.containsKey(start-1)) { - graph.put(start-1, new ArrayList<>()); - } - if(!graph.containsKey(end-1)) { - graph.put(end-1, new ArrayList<>()); - } graph.get(start-1).add(end-1); graph.get(end-1).add(start-1); cost.put(new Pair(start-1,end-1), edgeCost); } //System.out.println("Cost : "+cost); Map costWithHaybales = new HashMap<>(cost); - //System.out.println("Graph as an edgelist: "+graph); + System.out.println("Graph as an edgelist: "+graph); int[] taste = new int[K]; List bales = new ArrayList<>(); for(int i = 0; i < K; i ++) { @@ -89,11 +83,11 @@ public static void main(String[] args) throws IOException { costWithHaybales.put(new Pair(target,N), distOrig[target] - taste[i]); } //System.out.println("Modified New Graph : "+graph); - //System.out.println("Cost : "+cost); - //System.out.println("Cost with haybales: "+costWithHaybales); + System.out.println("Cost : "+cost); + System.out.println("Cost with haybales: "+costWithHaybales); distTo = dijkstra(costWithHaybales, graph, N+1, N); - //System.out.println("Output :" + Arrays.toString(distOrig)); - //System.out.println("Output(haybales):" + Arrays.toString(distTo)); + System.out.println("Output :" + Arrays.toString(distOrig)); + System.out.println("Output(haybales):" + Arrays.toString(distTo)); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dining.out"))); for(int i =0 ; i < numOfCows; i ++) { if(distTo[i] <= distOrig[i]) { From f6a18e1bdc6dcd8c7ba88b4643825133d86e79f6 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 12:58:16 -0700 Subject: [PATCH 14/27] check changes --- dining.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/dining.java b/dining.java index 90436c6..f6a5d9f 100644 --- a/dining.java +++ b/dining.java @@ -4,19 +4,20 @@ public class dining { public static int[] dijkstra(Map cost, List> graph, final int N, int source) { int[] dist = new int[N]; Set visited = new HashSet<>(); - PriorityQueue nodeQueue = new PriorityQueue<>(new Comparator() { + /*PriorityQueue nodeQueue = new PriorityQueue<>(N,new Comparator() { @Override public int compare(Integer arg0, Integer arg1) { return Integer.compare(dist[arg0], dist[arg1]); //return 0; - } + }*/ }); Arrays.fill(dist, Integer.MAX_VALUE); dist[N-1] = 0; // Starting value - nodeQueue.add(N-1); - while(visited.size() < N) { - int u = nodeQueue.remove(); // Get next node - visited.add(u); + //nodeQueue.add(N-1); + visited.add(N-1); + while(!visited.isEmpty()) { + //int u = nodeQueue.remove(); // Get next node + u = visited.first; List adj = graph.get(u); for(int node:adj) { if(!visited.contains(node)) { @@ -24,7 +25,7 @@ public int compare(Integer arg0, Integer arg1) { int totalDist = dist[u] + edgeDist; if (totalDist < dist[node]) dist[node] = totalDist; - nodeQueue.add(node); + //nodeQueue.add(node); } } } @@ -38,8 +39,8 @@ public static void main(String[] args) throws IOException { M = Integer.parseInt(st.nextToken()); K = Integer.parseInt(st.nextToken()); final int numOfCows = N - 1; - List> graph = new ArrayList<>(N+1); - for(int i = 0; i < N+1; i ++) { + List> graph = new ArrayList<>(M+1); + for(int i = 0; i < M+1; i ++) { graph.add(new ArrayList<>()); } Map cost = new HashMap<>(); From 9265cc472f700e847fbc72b8b3993861f6b2b07a Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 13:12:02 -0700 Subject: [PATCH 15/27] Change implementation incorrect --- dining.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/dining.java b/dining.java index f6a5d9f..2e823c6 100644 --- a/dining.java +++ b/dining.java @@ -3,28 +3,31 @@ public class dining { public static int[] dijkstra(Map cost, List> graph, final int N, int source) { int[] dist = new int[N]; - Set visited = new HashSet<>(); + Queue visited = new LinkedList<>(); /*PriorityQueue nodeQueue = new PriorityQueue<>(N,new Comparator() { @Override public int compare(Integer arg0, Integer arg1) { return Integer.compare(dist[arg0], dist[arg1]); //return 0; - }*/ - }); + } + }); + */ Arrays.fill(dist, Integer.MAX_VALUE); dist[N-1] = 0; // Starting value //nodeQueue.add(N-1); - visited.add(N-1); + visited.add(source); + int u; while(!visited.isEmpty()) { //int u = nodeQueue.remove(); // Get next node - u = visited.first; + u = visited.poll(); List adj = graph.get(u); for(int node:adj) { - if(!visited.contains(node)) { + if(dist[node] == Integer.MAX_VALUE) { // If this node has not been visited yet int edgeDist = cost.get(new Pair(u,node)); int totalDist = dist[u] + edgeDist; if (totalDist < dist[node]) dist[node] = totalDist; + visited.add(node); //nodeQueue.add(node); } } From 99be1575b81bc3782aaa6719f255114776365c87 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 13:41:37 -0700 Subject: [PATCH 16/27] Incorrect answer for first case. Broken? Yep --- dining.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dining.java b/dining.java index 2e823c6..fda6ac7 100644 --- a/dining.java +++ b/dining.java @@ -3,15 +3,14 @@ public class dining { public static int[] dijkstra(Map cost, List> graph, final int N, int source) { int[] dist = new int[N]; - Queue visited = new LinkedList<>(); - /*PriorityQueue nodeQueue = new PriorityQueue<>(N,new Comparator() { + PriorityQueue visited = new PriorityQueue<>(N,new Comparator() { @Override public int compare(Integer arg0, Integer arg1) { return Integer.compare(dist[arg0], dist[arg1]); //return 0; } }); - */ + Arrays.fill(dist, Integer.MAX_VALUE); dist[N-1] = 0; // Starting value //nodeQueue.add(N-1); @@ -35,7 +34,7 @@ public int compare(Integer arg0, Integer arg1) { return dist; } public static void main(String[] args) throws IOException { - BufferedReader f = new BufferedReader(new FileReader("3.in")); + BufferedReader f = new BufferedReader(new FileReader("1.in")); StringTokenizer st = new StringTokenizer(f.readLine()); int N,M,K; N = Integer.parseInt(st.nextToken()); From 8f56d9ebcb33c5af1ec62ae24fe131e01c9c897b Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 14:34:16 -0700 Subject: [PATCH 17/27] Update logic --- dining.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/dining.java b/dining.java index fda6ac7..0502d6b 100644 --- a/dining.java +++ b/dining.java @@ -18,15 +18,18 @@ public int compare(Integer arg0, Integer arg1) { int u; while(!visited.isEmpty()) { //int u = nodeQueue.remove(); // Get next node - u = visited.poll(); + u = visited.remove(); List adj = graph.get(u); for(int node:adj) { - if(dist[node] == Integer.MAX_VALUE) { // If this node has not been visited yet + //if(dist[node] == Integer.MAX_VALUE) + { // If this node has not been visited yet int edgeDist = cost.get(new Pair(u,node)); int totalDist = dist[u] + edgeDist; if (totalDist < dist[node]) + { dist[node] = totalDist; visited.add(node); + } //nodeQueue.add(node); } } @@ -102,7 +105,7 @@ public static void main(String[] args) throws IOException { pw.close(); } } -// Order does not matter pair +// Order does matter pair class Pair{ int x,y; public Pair(int x,int y) { @@ -117,7 +120,7 @@ public String toString() { public boolean equals(Object obj) { if(obj instanceof Pair) { Pair p = (Pair) obj; - if((p.x == this.x && p.y == this.y) || (p.x == this.y && this.x == p.y)) { + if((p.x == this.x && p.y == this.y)) { return true; } } @@ -125,6 +128,6 @@ public boolean equals(Object obj) { } @Override public int hashCode(){ - return Integer.hashCode(this.x) * Integer.hashCode(this.y); + return Integer.hashCode(this.x) * (Integer.hashCode(this.y)+1); } } \ No newline at end of file From db8e926cd6a195727a84b44d1b8257a2381a1b46 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Mon, 2 Sep 2019 14:37:55 -0700 Subject: [PATCH 18/27] order matters now --- dining.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dining.java b/dining.java index 0502d6b..c1178e9 100644 --- a/dining.java +++ b/dining.java @@ -57,6 +57,7 @@ public static void main(String[] args) throws IOException { graph.get(start-1).add(end-1); graph.get(end-1).add(start-1); cost.put(new Pair(start-1,end-1), edgeCost); + cost.put(new Pair(end-1,start-1), edgeCost); } //System.out.println("Cost : "+cost); Map costWithHaybales = new HashMap<>(cost); @@ -87,6 +88,7 @@ public static void main(String[] args) throws IOException { //System.out.println("Dist: "+distOrig[target]+" Taste: "+taste[i]); //System.out.println(target+" -- "+(N-1) + " connected with weight "+(distOrig[target] - taste[i])); costWithHaybales.put(new Pair(target,N), distOrig[target] - taste[i]); + //costWithHaybales.put(new Pair(N,target), distOrig[target] - taste[i]); } //System.out.println("Modified New Graph : "+graph); System.out.println("Cost : "+cost); From 3389cfc7d3bbc8f4cd1cfa42b76593837c4fded0 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sat, 28 Sep 2019 22:12:55 -0700 Subject: [PATCH 19/27] Add files via upload --- multimoo.java | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 multimoo.java diff --git a/multimoo.java b/multimoo.java new file mode 100644 index 0000000..f63cf8a --- /dev/null +++ b/multimoo.java @@ -0,0 +1,155 @@ +import java.io.*; +import java.util.*; +public class multimoo { + static Queue qx = new LinkedList<>(); + static Queue qy = new LinkedList<>(); + static Set visited = new HashSet<>(); + public static int[][] grid; + public static int N; + public static int floodFill(int tx,int ty, Segement s) { + visited.clear(); + int covered = 0; + //reset(); + int color = grid[tx][ty]; + qx.add(tx); + qy.add(ty); + while(!qx.isEmpty()) { + covered++; + int x = qx.poll(); + int y = qy.poll(); + s.pieces.add(new Position(x,y)); + //System.out.println(x+" "+y+" "+visited); + visited.add(new Position(x,y)); + if(x-1 >= 0 && x-1 < N && y < N && y >= 0 && grid[x-1][y] == color&& !visited.contains(new Position(x-1,y))) { + qx.add(x-1); + qy.add(y); + } + if(x+1 >= 0 && x+1 < N && y < N && y >= 0 && grid[x+1][y] == color&& !visited.contains(new Position(x+1,y))) { + qx.add(x+1); + qy.add(y); + } + if(x >= 0 && x < N && y +1 < N && y+1 >= 0 && grid[x][y+1] == color&& !visited.contains(new Position(x,y+1))) { + qx.add(x); + qy.add(y+1); + } + if(x >= 0 && x < N && y-1 < N && y-1 >= 0 && grid[x][y-1] == color && !visited.contains(new Position(x,y-1))) { + qx.add(x); + qy.add(y-1); + } + } + return covered; + } + + public static void main(String[] args) throws IOException{ + BufferedReader f = new BufferedReader(new FileReader("multimoo.in")); + N = Integer.parseInt(f.readLine()); + grid = new int[N][N]; + for(int i = 0; i < N; i ++) { + StringTokenizer st = new StringTokenizer(f.readLine()); + for(int j = 0 ; j < N; j ++) { + grid[i][j] = Integer.parseInt(st.nextToken()); + } + } + f.close(); + int maxID = 0; + int maxCount = 0; + List pieceList = new ArrayList<>(); + for(int i = 0; i < N; i ++) { + for(int j = 0; j < N; j ++) { + Segement s = new Segement(grid[i][j]); + if(!visited.contains(new Position(i,j))) { + int id = grid[i][j]; + int count = floodFill(i, j,s); + if(count > maxCount) { + maxCount = count; + maxID = id; + } + } + if(s.pieces.size() > 0) { + pieceList.add(s); + } + } + } + int LSize = 0; + for(Segement s:pieceList) { + System.out.println(s.pieces); + } + for(int i = 0; i < N; i ++) { + Segement p = pieceList.get(i); + for(Position pos:p.pieces) { + int x = pos.x; + int y = pos.y; + int color= grid[x][y]; + Set check = new HashSet(); + if(x-1 >= 0 && x-1 < N && y < N && y >= 0 && grid[x-1][y] != color&& !visited.contains(new Position(x-1,y))) { + check.add(new Position(x-1,y)); + } + if(x+1 >= 0 && x+1 < N && y < N && y >= 0 && grid[x+1][y] != color&& !visited.contains(new Position(x+1,y))) { + check.add(new Position(x+1,y)); + } + if(x >= 0 && x < N && y +1 < N && y+1 >= 0 && grid[x][y+1] != color&& !visited.contains(new Position(x,y+1))) { + check.add(new Position(x,y+1)); + } + if(x >= 0 && x < N && y-1 < N && y-1 >= 0 && grid[x][y-1] != color && !visited.contains(new Position(x,y-1))) { + check.add(new Position(x,y-1)); + } + for(int j = 0; j < pieceList.size(); j ++) { + if(i==j) { + continue; + } + Set checked = new HashSet<>(check); + checked.retainAll(pieceList.get(j).pieces); + if(checked.size() > 0) { + int size = pieceList.get(j).pieces.size() + pieceList.get(i).pieces.size(); + System.out.println("1: "+i+" "+j+" "+size); + System.out.println("2: "+pieceList.get(i).id+" "+pieceList.get(j).id+" "+size); + if(size>LSize) { + LSize = size; + } + } + checked.clear(); + checked = null; + } + } + } + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("multimoo.out"))); + System.out.println("Max Count "+maxCount+" (obtained with id "+maxID+")"); + System.out.println("Max Count team up "+LSize); + pw.println(maxCount); + pw.println(LSize); + pw.close(); + } + +} +class Position{ + int x,y; + //boolean perimeter = false; + public Position(int x,int y){ + this.x = x; + this.y = y; + } + @Override + public boolean equals(Object s) { + if(s instanceof Position) { + Position p = (Position) s; + return ((p.y == this.y) && (p.x == this.x)); + } + return false; + } + @Override + public int hashCode() { + return ((Integer.hashCode(this.x) -7)*(Integer.hashCode(this.y)-5)); + } + public String toString() { + return "("+this.x+","+this.y+")"; + } +} +class Segement{ + public Set pieces; + public int id; + public Segement(int id) { + this.id = id; + this.pieces = new HashSet<>(); + } + +} \ No newline at end of file From f5811afb0bdf4067898dcff8eb50149de0e7f91a Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sat, 28 Sep 2019 22:20:47 -0700 Subject: [PATCH 20/27] Add files via upload From 1d48bfab508825ebe4208fe02d079a4ad4d6c98f Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sat, 7 Dec 2019 20:52:26 -0800 Subject: [PATCH 21/27] Add files via upload --- convention.java | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 convention.java diff --git a/convention.java b/convention.java new file mode 100644 index 0000000..a169a20 --- /dev/null +++ b/convention.java @@ -0,0 +1,92 @@ + +import java.io.*; +import java.util.*; +public class convention { + public static void endProgram(int answer) throws IOException{ + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("convention.out"))); + pw.println(answer); + pw.close(); + System.exit(0); + } + public static void main(String[] args) throws IOException{ + BufferedReader f = new BufferedReader(new FileReader("convention1.in")); + StringTokenizer st = new StringTokenizer(f.readLine()); + int N,M,C; + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + C = Integer.parseInt(st.nextToken()); + st = new StringTokenizer(f.readLine()); + f.close(); + System.out.println(N+" "+M+" "+C); + List arrivTime = new ArrayList<>(); + for(int i = 0; i < N; i ++) { + arrivTime.add(Integer.parseInt(st.nextToken())); + } + if(C == 1) { + endProgram(N); + } + arrivTime.sort(null); + List options = new ArrayList<>(); + int required = 0; + for(int i = 0; i < N-C; i ++) { + //System.out.println("Pos: "+(i+C)+" , "+i); + //System.out.println("Dif: "+arrivTime.get(i+C)+" , "+arrivTime.get(i)); + int diff = arrivTime.get(i+C) - arrivTime.get(i); + //System.out.println(diff); + options.add(new PriorityElem(i,diff)); + // Pick diffs + } + //Collections.reverse(arrivTime); + //System.out.println(arrivTime); + options.sort(null); + for(PriorityElem pe: options) { + //System.out.println(pe.item + " "+pe.priority); + } + int answer = 0; + while(true) { + // Pick Optimally? + if(required > N - (answer*2)) { + break; + } + PriorityElem p = options.remove(0); + answer += 1; + required += C-2; + } + System.out.println(""); + endProgram(answer); + } + +} + +class Cow implements Comparator{ + int id; + int aTime; + public Cow(int id, int aTime) { + this.id = id; + this.aTime = aTime; + } + @Override + public int compare(Cow arg0, Cow arg1) { + // TODO Auto-generated method stub + return Integer.compare(arg0.aTime, arg1.aTime); + } +} +class PriorityElem implements Comparator, Comparable{ + public int multiplier = 1; + public int priority; + public int item; + public PriorityElem(int item, int priority) { + this.item = item; + this.priority = priority; + } + @Override + public int compare(PriorityElem arg0, PriorityElem arg1) { + // TODO Auto-generated method stub + return Integer.compare(arg0.priority, arg1.priority); + } + @Override + public int compareTo(PriorityElem arg0) { + + return this.compare(this, arg0); + } +} \ No newline at end of file From 1f4c498e581bc2729a9d1d898a9b3aad83ec5288 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sun, 8 Dec 2019 20:32:43 -0800 Subject: [PATCH 22/27] Nonworking new version --- convention.java | 55 ++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/convention.java b/convention.java index a169a20..a784fc5 100644 --- a/convention.java +++ b/convention.java @@ -2,6 +2,24 @@ import java.io.*; import java.util.*; public class convention { + public static boolean check(int test,int M, int C, List buses) { + int busStart=buses.get(0); + int busCount = 1; + int cowCount = 0; + for(int i = 0; i < buses.size(); i ++) { + if(busCount > M) { + return false; + } + int time = buses.get(i); + if(time - busStart > test || cowCount == C) { + busCount ++; + busStart = time; + cowCount = 0; + } + cowCount++; + } + return true; + } public static void endProgram(int answer) throws IOException{ PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("convention.out"))); pw.println(answer); @@ -22,38 +40,19 @@ public static void main(String[] args) throws IOException{ for(int i = 0; i < N; i ++) { arrivTime.add(Integer.parseInt(st.nextToken())); } - if(C == 1) { - endProgram(N); - } arrivTime.sort(null); - List options = new ArrayList<>(); - int required = 0; - for(int i = 0; i < N-C; i ++) { - //System.out.println("Pos: "+(i+C)+" , "+i); - //System.out.println("Dif: "+arrivTime.get(i+C)+" , "+arrivTime.get(i)); - int diff = arrivTime.get(i+C) - arrivTime.get(i); - //System.out.println(diff); - options.add(new PriorityElem(i,diff)); - // Pick diffs - } - //Collections.reverse(arrivTime); - //System.out.println(arrivTime); - options.sort(null); - for(PriorityElem pe: options) { - //System.out.println(pe.item + " "+pe.priority); - } - int answer = 0; + // 123456789 + int max = 1000000000; + int min = 0; while(true) { - // Pick Optimally? - if(required > N - (answer*2)) { - break; + int mid = (int) Math.floor((min + max)/2); + if(check(mid,M,C,arrivTime)) { + max = mid; + }else { + min = mid; } - PriorityElem p = options.remove(0); - answer += 1; - required += C-2; + System.out.println(min+" "+mid+" "+max); } - System.out.println(""); - endProgram(answer); } } From 7a2ead94f9b3c44aa4f87b112416844308e17660 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Thu, 19 Dec 2019 16:17:53 -0800 Subject: [PATCH 23/27] Add files via upload --- meetings.in | 4 ++ meetings.java | 182 ++++++++++++++++++++++++++++++++++++++++++++++++ meetings.out | 1 + milkvisits.in | 11 +++ milkvisits.java | 89 +++++++++++++++++++++++ milkvisits.out | 1 + moobuzz.in | 1 + moobuzz.java | 30 ++++++++ moobuzz.out | 1 + 9 files changed, 320 insertions(+) create mode 100644 meetings.in create mode 100644 meetings.java create mode 100644 meetings.out create mode 100644 milkvisits.in create mode 100644 milkvisits.java create mode 100644 milkvisits.out create mode 100644 moobuzz.in create mode 100644 moobuzz.java create mode 100644 moobuzz.out diff --git a/meetings.in b/meetings.in new file mode 100644 index 0000000..e3ab96c --- /dev/null +++ b/meetings.in @@ -0,0 +1,4 @@ +3 5 +1 1 1 +2 2 -1 +3 3 -1 \ No newline at end of file diff --git a/meetings.java b/meetings.java new file mode 100644 index 0000000..dc1b5f3 --- /dev/null +++ b/meetings.java @@ -0,0 +1,182 @@ +import java.io.*; +import java.util.*; +public class meetings { + public static void main(String[] args) throws IOException { + BufferedReader f = new BufferedReader(new FileReader("meetings.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("meetings.out"))); + int stoppedWeight = 0; + StringTokenizer st = new StringTokenizer(f.readLine()); + int N = Integer.parseInt(st.nextToken()); + int L = Integer.parseInt(st.nextToken()); + //List eventSeq = new ArrayList<>(); + List cows = new ArrayList<>(); + double halfSumWeight = 0; + int preP=0; + for(int i =0 ; i < N; i ++) { + st = new StringTokenizer(f.readLine()); + int w,v,p,d; + w = Integer.parseInt(st.nextToken()); + p = Integer.parseInt(st.nextToken())*2; + v = Integer.parseInt(st.nextToken()); + d = 0; + //System.out.println(v); + cows.add(new Cow(w,p,v, d)); + halfSumWeight += w; + } + halfSumWeight = halfSumWeight/2; + L = 2*L; + + cows.sort(null); + preP = cows.get(0).P; + int minDis; + minDis = L + 1; + for(int i =1 ; i < cows.size(); i ++) { + int d = cows.get(i).P - preP; + cows.get(i).D = d; + if(cows.get(i-1).V == 1&& cows.get(i).V==-1) + { + if(d= halfSumWeight) { + break; + } + + preP = cows.get(0).P; + minDis = L + 1; + for(i =1 ; i < cows.size(); i ++) { + int d = cows.get(i).P - preP; + cows.get(i).D = d; + if(cows.get(i-1).V == 1&& cows.get(i).V==-1) + { + if(d{ + public int W,P,V, D; + public Cow(int W,int P, int V, int D) { + this.W = W; + this.P = P; + this.V = V; + this.D = D; + } + @Override + public int compareTo(Cow o) { + return Integer.compare(this.P, o.P); + } + @Override + public String toString() { + return "#"+P+" moving at "+V+" with weight"+W; + } + +} +class Event implements Comparable{ + @Override + public String toString() { + return "@"+time+" "+cow1+" collides with "+cow2+" at pos "+pos; + } + public int time,pos,cow1,cow2; + public Event(int time, int pos, int cow1, int cow2) { + this.time = time; + this.pos = pos; + this.cow1 = cow1;this.cow2 = cow2; + } + @Override + public int compareTo(Event o) { + // TODO Auto-generated method stub + return Integer.compare(this.time, o.time); + } +} diff --git a/meetings.out b/meetings.out new file mode 100644 index 0000000..78c6bae --- /dev/null +++ b/meetings.out @@ -0,0 +1 @@ +2 diff --git a/milkvisits.in b/milkvisits.in new file mode 100644 index 0000000..b8cbd0b --- /dev/null +++ b/milkvisits.in @@ -0,0 +1,11 @@ +5 5 +HHGHG +1 2 +2 3 +2 4 +1 5 +1 4 H +1 4 G +1 3 G +1 3 H +5 5 H \ No newline at end of file diff --git a/milkvisits.java b/milkvisits.java new file mode 100644 index 0000000..d4503bb --- /dev/null +++ b/milkvisits.java @@ -0,0 +1,89 @@ +import java.io.*; +import java.util.*; +public class milkvisits { + public static void main(String[] args) throws IOException { + BufferedReader f = new BufferedReader(new FileReader("milkvisits.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("milkvisits.out"))); + StringTokenizer st = new StringTokenizer(f.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + char[] lookup = f.readLine().toCharArray(); + Map> connections = new HashMap<>(); + for(int i =0 ; i < N; i ++) { + connections.put(i+1, new ArrayList<>()); + } + for(int i = 0; i < N-1; i ++) { + st = new StringTokenizer(f.readLine()); + int x = Integer.parseInt(st.nextToken()); + int y = Integer.parseInt(st.nextToken()); + connections.get(x).add(y); + connections.get(y).add(x); + } + String answer = ""; + for(int i =0 ; i < M; i ++) { + st = new StringTokenizer(f.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + char pref = st.nextToken().charAt(0); + Set visited = new HashSet<>(); + Queue options = new LinkedList<>(); + List base = new ArrayList<>(); + base.add(a); + options.add(new Traversal(base, lookup[a-1] == pref)); + while(!options.isEmpty()) { + Traversal t =options.poll(); + int lastNode = t.nodes.get(t.nodes.size()-1); + if(lastNode == b) { + if(lookup[b-1] == pref || t.good) { + answer += "1"; + }else { + answer += "0"; + } + break; // FINALLY + } + for(int node: connections.get(lastNode)) { + if(visited.contains(node)) { + continue; + } + List al = new ArrayList<>(t.nodes); + al.add(node); + options.add(new Traversal(al, lookup[node-1] == pref || t.good)); + } + visited.add(lastNode); + } + } + pw.println(answer); + f.close(); + pw.close(); + } + +} +class Traversal { + List nodes; + boolean good; + public Traversal(List nodes, boolean gotMilk) { + this.good =gotMilk; + this.nodes = nodes; + } +} +class Path{ + public int x,y,w; + public Path(int x,int y) { + this.x = x; + this.y = y; + //this.w = length; + } + @Override + public boolean equals(Object o) { + if(o instanceof Path) { + Path p = (Path) o; + return (this.x == p.x)&&(this.y == p.y); + }else { + return false; + } + } + @Override + public int hashCode() { + return Integer.hashCode(this.x) * Integer.hashCode(this.y+1); + } +} \ No newline at end of file diff --git a/milkvisits.out b/milkvisits.out new file mode 100644 index 0000000..7c5652b --- /dev/null +++ b/milkvisits.out @@ -0,0 +1 @@ +10110 diff --git a/moobuzz.in b/moobuzz.in new file mode 100644 index 0000000..da2d398 --- /dev/null +++ b/moobuzz.in @@ -0,0 +1 @@ +14 \ No newline at end of file diff --git a/moobuzz.java b/moobuzz.java new file mode 100644 index 0000000..317dac5 --- /dev/null +++ b/moobuzz.java @@ -0,0 +1,30 @@ +import java.io.*; +import java.util.*; +public class moobuzz { + public static void main(String[] args) throws IOException { + BufferedReader f = new BufferedReader(new FileReader("moobuzz.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("moobuzz.out"))); + int N = Integer.parseInt(f.readLine()) -1; + int[] baseNumberTable = new int[9]; + int x = 0; + for(int i = 0; i < 9; i ++) { + while(true) { + x++; + if(x % 3 == 0 || x % 5 == 0) { + continue; + }else { + baseNumberTable[i] = x; + break; + } + } + } + //System.out.println(Arrays.toString(baseNumberTable)); + int num = N % 8; + int base = baseNumberTable[num]; + int offset = 15 * Math.floorDiv(N, 8); + pw.println(base + offset); + f.close(); + pw.close(); + } + +} diff --git a/moobuzz.out b/moobuzz.out new file mode 100644 index 0000000..d600f47 --- /dev/null +++ b/moobuzz.out @@ -0,0 +1 @@ +26 From 7a9a84b8bb182fae301ba68b32e7364152b78ee1 Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Fri, 20 Dec 2019 19:57:43 -0800 Subject: [PATCH 24/27] Add files via upload --- milkvisits.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/milkvisits.java b/milkvisits.java index d4503bb..e403e4b 100644 --- a/milkvisits.java +++ b/milkvisits.java @@ -27,12 +27,10 @@ public static void main(String[] args) throws IOException { char pref = st.nextToken().charAt(0); Set visited = new HashSet<>(); Queue options = new LinkedList<>(); - List base = new ArrayList<>(); - base.add(a); - options.add(new Traversal(base, lookup[a-1] == pref)); + options.add(new Traversal(a, lookup[a-1] == pref)); while(!options.isEmpty()) { Traversal t =options.poll(); - int lastNode = t.nodes.get(t.nodes.size()-1); + int lastNode = t.lastNode; if(lastNode == b) { if(lookup[b-1] == pref || t.good) { answer += "1"; @@ -45,9 +43,7 @@ public static void main(String[] args) throws IOException { if(visited.contains(node)) { continue; } - List al = new ArrayList<>(t.nodes); - al.add(node); - options.add(new Traversal(al, lookup[node-1] == pref || t.good)); + options.add(new Traversal(node, lookup[node-1] == pref || t.good)); } visited.add(lastNode); } @@ -59,11 +55,11 @@ public static void main(String[] args) throws IOException { } class Traversal { - List nodes; + int lastNode; boolean good; - public Traversal(List nodes, boolean gotMilk) { + public Traversal(int lastNode, boolean gotMilk) { this.good =gotMilk; - this.nodes = nodes; + this.lastNode = lastNode; } } class Path{ From cb1fa8f2b409ff2598312a008b07894b658d388b Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sat, 28 Dec 2019 12:10:23 -0800 Subject: [PATCH 25/27] Add ok working USACO milk visits solution --- milkvisits.java | 71 +++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/milkvisits.java b/milkvisits.java index e403e4b..06c9535 100644 --- a/milkvisits.java +++ b/milkvisits.java @@ -9,8 +9,8 @@ public static void main(String[] args) throws IOException { int M = Integer.parseInt(st.nextToken()); char[] lookup = f.readLine().toCharArray(); Map> connections = new HashMap<>(); - for(int i =0 ; i < N; i ++) { - connections.put(i+1, new ArrayList<>()); + for(int i =1 ; i < (N+1); i ++) { + connections.put(i, new ArrayList<>()); } for(int i = 0; i < N-1; i ++) { st = new StringTokenizer(f.readLine()); @@ -19,36 +19,50 @@ public static void main(String[] args) throws IOException { connections.get(x).add(y); connections.get(y).add(x); } - String answer = ""; - for(int i =0 ; i < M; i ++) { + int[] seg = new int[N]; + int segNum = 1; + Stack ts = new Stack<>(); + + + //int test = 0; + //char target = lookup[test]; + + for(int i = 1; i < (N+1); i ++) { + segNum++; + if(seg[i-1] != 0) { + continue; + } + char target = lookup[i-1]; + ts.push(new Traversal(i)); + while(!ts.empty()) { + Traversal t = ts.pop(); + seg[t.lastNode-1] = segNum; + for(int node: connections.get(t.lastNode)) { + if(lookup[node-1] == target && seg[node-1]==0) { + ts.push(new Traversal(node)); + } + } + } + } + //System.out.println("Node numbers : {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,...}"); + //System.out.println("Generated Seg Data: "+Arrays.toString(seg)); + //System.out.println("Lookup database : "+Arrays.toString(lookup)); + for(int i = 0; i < M; i ++) { st = new StringTokenizer(f.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); - char pref = st.nextToken().charAt(0); - Set visited = new HashSet<>(); - Queue options = new LinkedList<>(); - options.add(new Traversal(a, lookup[a-1] == pref)); - while(!options.isEmpty()) { - Traversal t =options.poll(); - int lastNode = t.lastNode; - if(lastNode == b) { - if(lookup[b-1] == pref || t.good) { - answer += "1"; - }else { - answer += "0"; - } - break; // FINALLY - } - for(int node: connections.get(lastNode)) { - if(visited.contains(node)) { - continue; - } - options.add(new Traversal(node, lookup[node-1] == pref || t.good)); + char T = st.nextToken().charAt(0); + if(seg[a-1] != seg[b-1]) { + pw.print("1"); + }else { + if(lookup[a-1] == T && lookup[b-1] == T) { + pw.print("1"); + }else { + pw.print("0"); } - visited.add(lastNode); } } - pw.println(answer); + pw.println(); f.close(); pw.close(); } @@ -56,9 +70,8 @@ public static void main(String[] args) throws IOException { } class Traversal { int lastNode; - boolean good; - public Traversal(int lastNode, boolean gotMilk) { - this.good =gotMilk; + + public Traversal(int lastNode) { this.lastNode = lastNode; } } From f6cb01444a568b370a9eebdeb99bd29e88e612dd Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sat, 28 Dec 2019 12:11:41 -0800 Subject: [PATCH 26/27] Update moobuzz.java --- moobuzz.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/moobuzz.java b/moobuzz.java index 317dac5..3584279 100644 --- a/moobuzz.java +++ b/moobuzz.java @@ -1,3 +1,6 @@ +// Extremely Simple MooBuzz solution +// Generates precalc lookup table +// Utilizes/"exploits" fact that the moobuzz sequence will repeat! import java.io.*; import java.util.*; public class moobuzz { From f0b0ded6d87ca3c563a3ccf19f8c02b1ae57218b Mon Sep 17 00:00:00 2001 From: Raymond <20248577+javaarchive@users.noreply.github.com> Date: Sun, 29 Dec 2019 11:56:44 -0800 Subject: [PATCH 27/27] template for meetings --- meetings.java | 194 +++++++------------------------------------------- 1 file changed, 25 insertions(+), 169 deletions(-) diff --git a/meetings.java b/meetings.java index dc1b5f3..90d1956 100644 --- a/meetings.java +++ b/meetings.java @@ -1,182 +1,38 @@ import java.io.*; import java.util.*; public class meetings { - public static void main(String[] args) throws IOException { - BufferedReader f = new BufferedReader(new FileReader("meetings.in")); - PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("meetings.out"))); - int stoppedWeight = 0; + + public static void main(String[] args) throws IOException{ + // TODO Auto-generated method stub + BufferedReader f= new BufferedReader(new FileReader("meetings.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("meetings.in"))); + int N, L; StringTokenizer st = new StringTokenizer(f.readLine()); - int N = Integer.parseInt(st.nextToken()); - int L = Integer.parseInt(st.nextToken()); - //List eventSeq = new ArrayList<>(); - List cows = new ArrayList<>(); - double halfSumWeight = 0; - int preP=0; - for(int i =0 ; i < N; i ++) { - st = new StringTokenizer(f.readLine()); - int w,v,p,d; - w = Integer.parseInt(st.nextToken()); - p = Integer.parseInt(st.nextToken())*2; - v = Integer.parseInt(st.nextToken()); - d = 0; - //System.out.println(v); - cows.add(new Cow(w,p,v, d)); - halfSumWeight += w; - } - halfSumWeight = halfSumWeight/2; - L = 2*L; - - cows.sort(null); - preP = cows.get(0).P; - int minDis; - minDis = L + 1; - for(int i =1 ; i < cows.size(); i ++) { - int d = cows.get(i).P - preP; - cows.get(i).D = d; - if(cows.get(i-1).V == 1&& cows.get(i).V==-1) - { - if(d c = new ArrayList<>(); + for(int i = 0 ; i < N ; i++) { + int w = Integer.parseInt(st.nextToken()); + int x = Integer.parseInt(st.nextToken()); + int d = Integer.parseInt(st.nextToken()); + c.add(new Cow(x,d,w)); } - - if(cows.get(cows.size()-1).V == 1) - { - int d = cows.get(cows.size()-1).P; - if(d= halfSumWeight) { - break; - } - - preP = cows.get(0).P; - minDis = L + 1; - for(i =1 ; i < cows.size(); i ++) { - int d = cows.get(i).P - preP; - cows.get(i).D = d; - if(cows.get(i-1).V == 1&& cows.get(i).V==-1) - { - if(d{ - public int W,P,V, D; - public Cow(int W,int P, int V, int D) { - this.W = W; - this.P = P; - this.V = V; - this.D = D; - } - @Override - public int compareTo(Cow o) { - return Integer.compare(this.P, o.P); - } - @Override - public String toString() { - return "#"+P+" moving at "+V+" with weight"+W; - } - -} -class Event implements Comparable{ - @Override - public String toString() { - return "@"+time+" "+cow1+" collides with "+cow2+" at pos "+pos; - } - public int time,pos,cow1,cow2; - public Event(int time, int pos, int cow1, int cow2) { - this.time = time; +class Cow{ + int pos; + int velocity; + int weight; + public Cow(int pos, int v, int w) { + this.weight = w; + this.velocity = v; this.pos = pos; - this.cow1 = cow1;this.cow2 = cow2; } - @Override - public int compareTo(Event o) { - // TODO Auto-generated method stub - return Integer.compare(this.time, o.time); - } -} +} \ No newline at end of file