美文网首页
杭电acm1863 畅通工程

杭电acm1863 畅通工程

作者: cwhong | 来源:发表于2018-06-28 08:57 被阅读0次

畅通工程

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33566 Accepted Submission(s): 14848

Problem Description

省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
 
Input
 
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
 
Output
 
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
 
Sample Input
 
3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100
 
Sample Output
 
3
 
?

这道题用最小生成树和查并集来解,并查集https://baike.baidu.com/item/%E5%B9%B6%E6%9F%A5%E9%9B%86/9388442?fr=aladdin百度百科中有源代码 使用并查集生成树后若其中有不联通的点那么有两个以上的点父节点为其自身

package acm1863;
 
/**
 * date:2017.12.12
 * author:孟小德
 * function:    杭电acm1863
 *      畅通工程    并查集,最小生成树
 */
 
import java.util.*;
 
class Edge implements Comparable<Edge>
{
    public int start,end,cost;
 
    public Edge(int start,int end,int cost)
    {
        this.start = start;
        this.end = end;
        this.cost = cost;
    }
 
    public Edge()
    {
 
    }
 
    public int compareTo(Edge otherEdge)
    {
        return this.cost - otherEdge.cost;
    }
}
 
public class Main
{
 
    public static int NUM_OF_NODE,NUM_OF_EDGE,RESULT;
    public static ArrayList<Edge> LIST_OF_EDGE = new ArrayList<Edge>();
    public static int[] father;
 
    public static int getParent(int m)
    {
        //  寻找节点的根
        while(father[m] != m)
        {
            m = father[m];
        }
 
        return m;
    }
 
    public static int union(int x,int y,int c)
    {
        //  找到两个节点的根若根相同则两节点已联通
        //  否则未联通,将节点链接
        int a = getParent(x);
        int b = getParent(y);
 
        if (a != b)
        {
            father[a] = b;
            return c;
        }
        return 0;
    }
 
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
 
        while ((NUM_OF_EDGE = input.nextInt()) != 0)
        {
            NUM_OF_NODE = input.nextInt();
            father = new int[NUM_OF_NODE + 1];
            for (int i=1;i<=NUM_OF_NODE;i++)
            {
                //  初始化根数组,每个节点初始根都是其本身
                father[i] = i;
            }
 
            for (int i=0;i<NUM_OF_EDGE;i++)
            {
                int s = input.nextInt();
                int e = input.nextInt();
                int c = input.nextInt();
 
                LIST_OF_EDGE.add(new Edge(s,e,c));
            }
            Collections.sort(LIST_OF_EDGE);//对边进行排序
            RESULT = 0;
            for (int i=0;i<NUM_OF_EDGE;i++)
            {
                int CUR_S = LIST_OF_EDGE.get(i).start;
                int CUR_E = LIST_OF_EDGE.get(i).end;
                int CUR_C = LIST_OF_EDGE.get(i).cost;
                RESULT += union(CUR_S,CUR_E,CUR_C);
 
            }
 
            int num = 0;
            for (int i=1;i<=NUM_OF_NODE;i++)
            {
                if (father[i] == i)
                {
                    num++;
                }
            }
            if (num > 1)
            {
                System.out.println("?");
            }
            else
            {
                System.out.println(RESULT);
            }
            LIST_OF_EDGE.clear();
        }
 
        input.close();
    }
}

相关文章

  • 杭电acm1863 畅通工程

    畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Lim...

  • 额外 - 杭电 - 畅通工程

    题目:畅通工程 本质:计算无向图的连通分量 输入:int N = 城镇数list input_list = 城镇...

  • 杭电acm1232 畅通工程

    畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Lim...

  • 杭电acm1874 畅通工程续

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Li...

  • 杭电oj-1233-还是畅通工程

    题目链接:还是畅通工程prim算法原理注意点:本文针对的题目还是畅通工程中每两个顶点间都是有一条非零的连接,即共n...

  • 并查集

    CSDN上的并查集讲解的非常清楚,这里只做补充。 CSDN上的并查集讲解 这是杭电OJ上对应的题目:畅通工程 关于...

  • 畅通工程

    解析 题面中描述的是一个实际的问题, 但该问题可以被抽象成在一个图上查找连通分量(彼此连通的结点集合)的个数,我们...

  • HDU 1232 - 畅通工程

    畅通工程 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标...

  • 杭电助手

    杭电助手(服务号hduhelp,订阅号hduhelper)是隶属于杭州电子科技大学党委学工部的校级组织,我们有前端...

  • 杭电2015

    这道题看起来不复杂,但做起来还是挺费工夫的。里面要用很多的循环结构,很容易在些小地方出错。我就是因为那些小问题而搞...

网友评论

      本文标题:杭电acm1863 畅通工程

      本文链接:https://www.haomeiwen.com/subject/wmiwyftx.html