ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

图算法-MST最小生成树

2021-12-16 13:04:10  阅读:305  来源: 互联网

标签:city matrix int MST 最小 value 算法 cost cities


Minimum cost to connect all cities

  • Difficulty Level : Medium

  • There are n cities and there are roads in between some of the cities. Somehow all the roads are damaged simultaneously. We have to repair the roads to connect the cities again. There is a fixed cost to repair a particular road. Find out the minimum cost to connect all the cities by repairing roads. Input is in matrix(city) form, if city[i][j] = 0 then there is not any road between city i and city j, if city[i][j] = a > 0 then the cost to rebuild the path between city i and city j is a. Print out the minimum cost to connect all the cities. 

It is sure that all the cities were connected before the roads were damaged.

Examples: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input : {{0, 1, 2, 3, 4},
         {1, 0, 5, 0, 7},
         {2, 5, 0, 6, 0},
         {3, 0, 6, 0, 0},
         {4, 7, 0, 0, 0}};
Output : 10

 

import java.io.*;
import java.util.*;

class GFG {
    public static void main (String[] args) {
        int[][] matrix = new int[][]{{0, 1, 1, 100, 0, 0},
         {1, 0, 1, 0, 0, 0},
         {1, 1, 0, 0, 0, 0},
         {100, 0, 0, 0, 2, 2},
         {0, 0, 0, 2, 0, 2},
         {0, 0, 0, 2, 2, 0}};
        System.out.println(getMinPath(matrix));
    }
    static class Edge{
        int x;
        int y;
        int value;
        Edge(int x,int y,int value){
            this.x=x;
            this.y=y;
            this.value=value;
        }
    }
    private static int getMinPath(int[][] matrix){
        int len = matrix.length;
        //1.create heap
        PriorityQueue<Edge> pq = new PriorityQueue<>((a,b)->a.value-b.value);
        //2.put the first element into heap
        Set<Integer> visited = new HashSet<>();
        visited.add(0);
        for(int i=0;i<len;i++) if(i!=0 && matrix[0][i]!=0) pq.offer(new Edge(0,i,matrix[0][i]));
        //3.while loop 
        int sum = 0;
        while(!pq.isEmpty()){
            Edge edge = pq.poll();
            if(visited.add(edge.y)){
                sum+=edge.value;
                for(int i=0;i<len;i++) if(i!=edge.y && matrix[edge.y][i]!=0) pq.offer(new Edge(edge.y,i,matrix[edge.y][i]));
            }
        }
        if(visited.size()==len) return sum;
        return -1;
    }
}

 

标签:city,matrix,int,MST,最小,value,算法,cost,cities
来源: https://www.cnblogs.com/cynrjy/p/15697369.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有