zoukankan      html  css  js  c++  java
  • POJ 2421 Constructing Roads

    http://poj.org/problem?id=2421

    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

    题解:建好的边记为 0

    代码:

    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    #define inf 0x3f3f3f3f
    int N, P;
    int mp[110][110];
    bool vis[110];
    int dis[110];
    
    int prim() {
        memset(dis, inf, sizeof(dis));
        memset(vis, false, sizeof(vis));
    
        dis[1] = 0;
        int temp;
        int res = 0;
        for(int i = 1; i <= N; i ++)
            dis[i] = mp[1][i];
    
        for(int i = 1; i <= N; i ++) {
            int minn = inf;
            for(int j = 1; j <= N; j ++) {
                if(!vis[j] && dis[j] < minn) {
                    temp = j;
                    minn = dis[j];
                }
            }
            vis[temp] = true;
            for(int j = 1; j <= N; j ++)
                if(!vis[j])
                    dis[j] = min(dis[j], mp[temp][j]);
        }
    
        for(int i = 1; i <= N; i ++)
            res += dis[i];
    
        return res;
    }
    
    int main() {
        scanf("%d", &N);
        memset(mp, inf, sizeof(mp));
        for(int i = 1; i <= N; i ++) {
            for(int j = 1; j <= N; j ++) {
                int s;
                scanf("%d", &s);
                if(s < mp[i][j]) {
                    mp[i][j] = s;
                    mp[j][i] = mp[i][j];
                }
            }
        }
    
        int sum = 0;
        scanf("%d", &P);
        for(int i = 1; i <= P; i ++) {
            int a, b;
            scanf("%d%d", &a, &b);
            mp[a][b] = mp[b][a] = 0;
        }
    
        int ans = prim();
        printf("%d
    ", ans);
        return 0;
    }
    

      

  • 相关阅读:
    【问题】SUSE已经安装了libsodium,安装zeromq时出现下面的错误?
    将博客搬至CSDN
    iOS开发之self.abc = nil与[_abc release]的区别
    Swift编程语言入门教程
    如何使用NSData处理数据
    集合之間的轉換
    數組排序
    集合类的用法总结(NSArray、NSDictionary、NSSet)
    NSString的各种用法总结(创建、截取、判断比较、转化数据类型、拼接、替换、添加、追加、读取、写入、删去、改变)
    instancetype和id的区别
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9745136.html
Copyright © 2011-2022 走看看