Minimum Spanning Tree

Description

Given a list of Connections, which is the Connection class (the city name at both ends of the edge and a cost between them), find edges that can connect all the cities and spend the least amount.
Return the connects if can connect all the cities, otherwise return empty list.

Return the connections sorted by the cost, or sorted city1 name if their cost is same, or sorted city2 if their city1 name is also same.

Example

Example 1:

Input:
["Acity","Bcity",1]
["Acity","Ccity",2]
["Bcity","Ccity",3]
Output:
["Acity","Bcity",1]
["Acity","Ccity",2]

Example 2:

Input:
["Acity","Bcity",2]
["Bcity","Dcity",5]
["Acity","Dcity",4]
["Ccity","Ecity",1]
Output:
[]

Explanation:
No way
给出一些Connections,即Connections类,找到一些能够将所有城市都连接起来并且花费最小的边。
如果说可以将所有城市都连接起来,则返回这个连接方法;不然的话返回一个空列表。
思路:考察并查集。如果满足条件的连接边数不等于n - 1(n为节点数)则表示没有可行方法。使用HashMap(将每个节点对应一个id), 对id使用并查集。用map存储自动排序,再利用并查集,连通的点根应该相同。
/**
 * Definition for a Connection.
 * public class Connection {
 *   public String city1, city2;
 *   public int cost;
 *   public Connection(String city1, String city2, int cost) {
 *       this.city1 = city1;
 *       this.city2 = city2;
 *       this.cost = cost;
 *   }
 * }
 */
public class Solution {
    /**
     * @param connections given a list of connections include two cities and cost
     * @return a list of connections from results
     */
    private int[] father;
    private int find(int a) {
        int root = a;
        while (root != father[root]) {
            root = father[root];
        }
        
        while(a != root) {
            int tmp = father[a];
            father[a] = root;
            a = tmp;
        }
        return root;
    }
    
    static Comparator<Connection> comp = new Comparator<Connection>() {
            public int compare(Connection a, Connection b) {
                if (a.cost != b.cost) {
                    return a.cost - b.cost;
                }
                if (a.city1.equals(b.city1)) {
                    return a.city2.compareTo(b.city2);
                }
                return a.city1.compareTo(b.city1);
            }
        };
        
    public List<Connection> lowestCost(List<Connection> connections) {
        Collections.sort(connections, comp);
        Map<String, Integer> hash = new HashMap<>();
        ArrayList<Connection> result = new ArrayList<>();
        int id = 0;
        for (Connection conn : connections) {
            if (!hash.containsKey(conn.city1)) {
                hash.put(conn.city1, ++id);
            }
            
            if (!hash.containsKey(conn.city2)) {
                hash.put(conn.city2, ++id);
            }
        }
        
        father = new int[id + 1];
        for (int i = 1; i < id + 1; i++) {
            father[i] = i;
        }
        
        List<Connection> results = new ArrayList<>();
        for (Connection conn: connections) {
            int id1 = hash.get(conn.city1);
            int id2 = hash.get(conn.city2);
            
            int root1 = find(id1);
            int root2 = find(id2);
            if (root1 != root2) {
                father[root1] = father[root2];
                result.add(conn);
            }
        }
        
        if (result.size() != id - 1) {
            return new ArrayList<>();
        }
        return result;
        
    }
}

  



 

猜你喜欢

转载自www.cnblogs.com/FLAGyuri/p/12076322.html