Advertisement
uopspop

Untitled

Oct 3rd, 2021
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. /**
  2.  * Definition for a Connection.
  3.  * public class Connection {
  4.  *   public String city1, city2;
  5.  *   public int cost;
  6.  *   public Connection(String city1, String city2, int cost) {
  7.  *       this.city1 = city1;
  8.  *       this.city2 = city2;
  9.  *       this.cost = cost;
  10.  *   }
  11.  * }
  12.  */
  13. public class Solution {
  14.     /**
  15.      * @param connections given a list of connections include two cities and cost
  16.      * @return a list of connections from results
  17.      */
  18.  
  19.     public class MyComp implements Comparator<Connection> {
  20.         public int compare(Connection c1, Connection c2) {
  21.             if (c1.cost != c2.cost) {
  22.                 return c1.cost - c2.cost;
  23.             }
  24.             if (c1.city1.equals(c2.city1) == false) {
  25.                 return c1.city1.compareTo(c2.city1);
  26.             }
  27.             if (c1.city2.equals(c2.city2) == false) {
  28.                 return c1.city2.compareTo(c2.city2);
  29.             }
  30.             return 0; // should not reach here
  31.         }
  32.     }
  33.  
  34.     public List<Connection> lowestCost(List<Connection> connections) {
  35.         // Write your code here
  36.         UnionFind uf = new UnionFind();
  37.         Set<String> citySet = new HashSet<>();
  38.         List<Connection> result = new ArrayList<>();
  39.         PriorityQueue<Connection> conns = new PriorityQueue<>(new MyComp());
  40.         for (Connection c: connections) {
  41.             conns.offer(c);
  42.             uf.add(c.city1);
  43.             uf.add(c.city2);
  44.             citySet.add(c.city1);
  45.             citySet.add(c.city2);
  46.         }
  47.  
  48.         while (true) {
  49.             if (conns.size() == 0) break;
  50.  
  51.             Connection conn = conns.poll();
  52.             if (uf.is_connected(conn.city1, conn.city2)) continue;
  53.             result.add(conn);
  54.             uf.union(conn.city1, conn.city2);
  55.             // System.out.printf("(%d)%n", conn.cost);
  56.  
  57.         }
  58.  
  59.         // 圖論 - a tree will always have number_of_edges == (number_of_nodes - 1)
  60.         if (result.size() != (citySet.size() - 1)) return new ArrayList<>();
  61.  
  62.         Collections.sort(result, new MyComp());
  63.  
  64.         return result;
  65.     }
  66.  
  67.     class UnionFind {
  68.  
  69.         Map<String, String> city2parent = new HashMap<>();
  70.  
  71.         public void add(String c1) {
  72.             if (city2parent.containsKey(c1)) return;
  73.             city2parent.put(c1,c1); // be it's own parent as default
  74.         }
  75.  
  76.         public String find_root(String c1) {
  77.             if (city2parent.get(c1).equals(c1)) return c1;
  78.  
  79.             String root = find_root(city2parent.get(c1));
  80.             city2parent.put(c1, root);
  81.            
  82.             return root;
  83.         }
  84.  
  85.         public void merge(String c1, String c2) {
  86.             String root2 = find_root(c2);
  87.             city2parent.put(c1, root2);
  88.         }
  89.  
  90.         public void union(String c1, String c2) {
  91.  
  92.             add(c1);
  93.             add(c2);
  94.  
  95.             String root1 = find_root(c1);
  96.             String root2 = find_root(c2);
  97.  
  98.             if (root1.equals(root2)) return;
  99.  
  100.             merge(root1, root2);  // attention: merge is at root level
  101.  
  102.         }
  103.  
  104.         public boolean is_connected(String c1, String c2){
  105.             String root1 = find_root(c1);
  106.             String root2 = find_root(c2);
  107.  
  108.             if (root1.equals(root2)) return true;
  109.  
  110.             return false;
  111.         }
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement