Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7.  
  8. public class mrpresident {
  9. static int[] arrow;
  10.  
  11. static int find(int person) {
  12. if (arrow[person] == -1) {
  13. return person;
  14. }
  15. arrow[person] = find(arrow[person]);
  16. return arrow[person];
  17. }
  18.  
  19. static void combineGroup(int person1, int person2) {
  20. person1 = find(person1);
  21. person2 = find(person2);
  22. arrow[person1] = person2;
  23. }
  24.  
  25. static class Edge implements Comparable<Edge> {
  26. int cityA, cityB;// cities it connects
  27. long cost;// cost
  28.  
  29. @Override
  30. public int compareTo(Edge other) {
  31. return Long.compare(cost, other.cost);
  32. }
  33. }
  34.  
  35. public static void main(String[] args) throws IOException {
  36. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  37. String[] line = br.readLine().split(" ");
  38. int n = Integer.parseInt(line[0]);
  39. int m = Integer.parseInt(line[1]);
  40. long k = Long.parseLong(line[2]);
  41.  
  42. // initialize disjoint set
  43. arrow = new int[n + 6];
  44. Arrays.fill(arrow, -1);
  45.  
  46. ArrayList<Edge> roads = new ArrayList<Edge>();
  47.  
  48. // read in roads
  49. for (int i = 0; i < m; i++) {
  50. // read in line
  51. line = br.readLine().split(" ");
  52. Edge road = new Edge();
  53. road.cityA = Integer.parseInt(line[0]) - 1;
  54. road.cityB = Integer.parseInt(line[1]) - 1;
  55. road.cost = Long.parseLong(line[2]);
  56. roads.add(road);
  57. }
  58.  
  59. // sort from least to most expensive
  60. Collections.sort(roads);
  61.  
  62. // kruskals algorithm
  63. ArrayList<Edge> keptRoads = new ArrayList<Edge>();
  64. long totalCost = 0;
  65. for (Edge road : roads) {
  66. if (find(road.cityA) != find(road.cityB)) {
  67. keptRoads.add(road);
  68. totalCost += road.cost;
  69. combineGroup(road.cityA, road.cityB);
  70. }
  71. }
  72.  
  73. // check if possible
  74. if (keptRoads.size() != n - 1) {
  75. System.out.println(-1);
  76. return;
  77. }
  78.  
  79. Collections.sort(keptRoads);
  80. Collections.reverse(keptRoads);
  81.  
  82. // start from most expensive road, and see if we need to transform it to a super
  83. // road
  84. int superRoadsUsed = 0;
  85. for (Edge road : keptRoads) {
  86. if (totalCost <= k) {
  87. break;
  88. }
  89. totalCost -= road.cost;
  90. totalCost += 1;
  91. superRoadsUsed++;
  92. }
  93.  
  94. // not enough money
  95. if (totalCost > k) {
  96. System.out.println(-1);
  97. return;
  98. }
  99.  
  100. System.out.println(superRoadsUsed);
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement