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;
    }
  • 相关阅读:
    20165323《Java程序设计》第九周学习总结
    20165323 实验三 敏捷开发与XP实践
    20165323 结对编程之四则运算week2-整体总结
    20165334 20165311 20165329 实验四外设驱动程序设计
    实验四
    20165311 《信息安全系统设计基础》第七周学习
    20165311 《信息安全系统设计基础》第六周学习总结
    20165311 《信息安全系统设计基础》第四周学习总结
    20165311 20165334 20165329实验一 开发环境的熟悉
    2018 第三周 20165311 缓冲区溢出漏洞实验和第三周学习总结
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3376687.html
Copyright © 2011-2022 走看看