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;
    }
    

      

  • 相关阅读:
    PHP 类的继承问题
    爬虫第一章
    如何给CBV添加装饰器
    结巴分词 gensim系数矩阵相似度 pypinyin
    elasticsearch 第二章 elasticsearch的详细用法及参数
    运维自动化 第五章 playbook 模块补充
    运维自动化 第四章 模块
    运维自动化 第三章 ansible
    正则补充
    运维自动化 第二章 openpyxl的用法,读写excel内容
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9745136.html
Copyright © 2011-2022 走看看