zoukankan      html  css  js  c++  java
  • HDU 1102 Constructing Roads

    Constructing Roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11576    Accepted Submission(s): 4353

    Problem Description
    There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
    We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
     
    Input
    The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
    Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
     
    Output
    You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
     
    Sample Input
    3
    0 990 692
    990 0 179
    692 179 0
    1
    1 2
     
    Sample Output
    179
     
    Source
     
    Recommend
    Eddy
     
    思路:简单最小生成树,使用Kruskal,不知道为什么使用Prim总是WA
     
    代码:
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const int MAXN = 100010;
    int map[110][110];
    int a,b;
    int n,q;
    int USETree[110];
    int the_last_flag;
    int the_last_sum;
    struct Node
    {
        int first,end;
        int value;
        bool select;
    }Edge[MAXN];
    int find(int x)
    {
        while(x != USETree[x])
        {
            x = USETree[x];
        }
        return x;
    }
    void Merge(int x,int y)
    {
        int p = find(x);
        int q = find(y);
        if(p < q)
           USETree[q] = p;
        else
           USETree[p] = q;
    }
    bool cmp(Node a,Node b)
    {
        if(a.value != b.value)
            return a.value < b.value;
        if(a.first != b.first)
            return a.first < b.first;
        return a.end < b.end;
    }
    void Kruskal(Node *Egde,int n,int m)
    {
        the_last_flag = 0;
        sort(Edge + 1,Edge + m + 1,cmp);
        for(int i = 1;i <= m;i ++)
        {
            if(the_last_flag == n - 1)
               break ;
            int x = find(Edge[i].first);
            int y = find(Edge[i].end);
            if(x != y)
            {
                Merge(x,y);
                the_last_flag ++;
                Edge[i].select = true;
            }
        }
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(Edge,0,sizeof(Edge));
            for(int i = 1;i <= n;i ++)
               USETree[i] = i;
            for(int i = 1;i <= n;i ++)
               for(int j = 1;j <= n;j ++)
                  scanf("%d",&map[i][j]);
            scanf("%d",&q);
            while(q --)
            {
                scanf("%d%d",&a,&b);
                map[a][b] = 0;
                map[b][a] = 0;
            }
            int pos = 0;
            for(int i = 1;i <= n;i ++)
               for(int j = 1;j <= n;j ++)
               {
                   if(j > i)
                   {
                     Edge[++ pos].value = map[i][j];
                     Edge[pos].first = i;Edge[pos].end = j;
                   }
               }
            Kruskal(Edge,n,pos);
            the_last_sum = 0;
            for(int i = 1;i <= pos;i ++)
            {
              if(Edge[i].select == 1)
                 the_last_sum += Edge[i].value;
            }
            printf("%d
    ",the_last_sum);
        }
        return 0;
    }
  • 相关阅读:
    Linux学习笔记-Shell和命令基础
    转载 | 辗转相除法
    C语言字符数组应用示例2:用二维数组输出一个菱形图案
    C语言字符数组应用示例1:编写一个程序,将两个字符串连接起来,不用strcat函数。
    C语言字符数组超细讲解
    C语言二维数组的应用举例
    C语言二维数组超细讲解
    Java同步方法:synchronized到底锁住了谁?
    家乐的深度学习笔记「5」
    工程中的算法应用
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3376687.html
Copyright © 2011-2022 走看看