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

      

  • 相关阅读:
    jquery选择器
    jquery是方便了javascript,算是其子,导入方式必须记住,js文件自己官网下载
    checkbox操作全选、反选、取消
    onsubmit事件、事件传播判定函数stoppropagetion、事件使用2种方式
    onload函数绑定,byclass数组遍历、this获取整个标签内容
    区分byid和byclass的取值方法关键是有无数组、onfocus和onblur
    dom :document、element,如何取父标签和子标签,innertext获取标签内容
    定时器,setInterval、clearInterval
    一个简单css+js的开关组件
    Bootstrap 模态框强化
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9745136.html
Copyright © 2011-2022 走看看