fork download
  1. class Scratch {
  2. public static int numEdges;
  3. public static int queries;
  4. public static Edge[] paths;
  5. public static Edge[] archs;
  6.  
  7. public static void main(String[] args) {
  8. numEdges = 3;
  9. queries = 2;
  10.  
  11. paths = new Edge[numEdges];
  12. paths[0] = new Edge(1);
  13. paths[1] = new Edge(2);
  14. paths[2] = new Edge(3);
  15.  
  16. archs = new Edge[queries];
  17. archs[0] = new Edge(10);
  18. archs[1] = new Edge(10);
  19.  
  20. int pathIndex = 0;
  21.  
  22. // Reachable artifacts set
  23. for (int i = 0; i < queries; i++) {
  24. System.out.println(archs[i].getWeight());
  25. pathIndex = determineViablePaths(archs[i].getWeight());
  26.  
  27. }
  28. }
  29.  
  30. // Method to return last index of path that can be traversed
  31. public static int determineViablePaths(int weight) {
  32.  
  33. for (int i = 0; i < numEdges; i++) {
  34. if (paths[i].getWeight() <= weight)
  35. continue;
  36. else {
  37. return i - 1;
  38. }
  39. }
  40. return numEdges-1;
  41. }
  42.  
  43. // The edge class
  44. public static class Edge implements Comparable<Edge> {
  45.  
  46. int weight;
  47.  
  48. Edge(int weight) {
  49. this.weight = weight;
  50. }
  51.  
  52. public int getWeight() {
  53. return this.weight;
  54. }
  55.  
  56. // The Edge comparison method
  57. public int compareTo(Edge other) {
  58. if(this.weight < other.weight)
  59. return 0;
  60. else if (this.weight == other.weight)
  61. return 1;
  62. return -1 ;
  63. }
  64. }
  65. }
Success #stdin #stdout 0.06s 32292KB
stdin
Standard input is empty
stdout
10
10