zoukankan      html  css  js  c++  java
  • POJ 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads 

    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

    题意+题解 

    解释下样例吧

    输入N = 3 代表有N = 3个点 ,接下来N = 3行 每行都有N = 3个数,代表到每个点的距离

    1 - 1 距离为0 , 1 - 2 距离为990, 1-3 距离为692

    2 - 1距离为990 , 2 - 2 距离为0  ,2-3 距离为179

    3 - 1距离为692 , 3- 2 距离为179 , 3-3 距离为0    

    输入Q = 1 代表接下来有Q = 1行

    每行输入 两个点   代表两点已经连通  如 1 和 2直接已经连通

    那么要求最小生成树,先已连通的边进行并查集的合并操作 把剩下的边进行Kruscal即可

    代码

    #include<iostream>
    #include<cstdio>     //EOF,NULL
    #include<cstring>    //memset
    #include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
    #include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
    #include<algorithm>  //fill,reverse,next_permutation,__gcd,
    #include<string>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<utility>
    #include<iterator>
    #include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
    #include<functional>
    #include<map>
    #include<set>
    #include<limits.h>     //INT_MAX
    #include<bitset> // bitset<?> n
    using namespace std;
    
    typedef long long ll;
    typedef pair<int,int> P;
    #define all(x) x.begin(),x.end()
    
    #define readc(x) scanf("%c",&x)
    #define read(x) scanf("%d",&x)
    #define read2(x,y) scanf("%d%d",&x,&y)
    #define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    #define print(x) printf("%d
    ",x)
    #define mst(a,b) memset(a,b,sizeof(a))
    #define lowbit(x) x&-x
    #define lson(x) x<<1
    #define rson(x) x<<1|1
    #define pb push_back
    #define mp make_pair
    const int INF =0x3f3f3f3f;
    const int inf =0x3f3f3f3f;
    const int mod = 1e9+7;
    const int MAXN = 105;
    const int maxn = 10010;
    
    int n,m;
    int cnt ;
    int ans;
    int a,b,v;
    int pre[MAXN];
    
    struct node{
      int st,ed,v;
      bool operator < (node b) const{
         return v < b.v;
      }
    }rod[maxn];
    
    void Init(){
      ans = 0;
      cnt = 0;
      for(int i = 0 ; i < n; i++){
        pre[i] = i;
      }
    }
    int find(int x){ return x == pre[x] ? x : pre[x] = find(pre[x]);}
    bool join(int x,int y){
        if(find(x)!=find(y)){
          pre[find(y)] = find(x);
          return true;
        }
        return false;
    }
    void kruskal(){
        for(int i = 0 ;i < cnt ; i++){
        int mp1 = find(rod[i].st);
        int mp2 = find(rod[i].ed);
        if(join(mp1,mp2)) ans+= rod[i].v;
      }
    }
    int main(){
          read(n) ;
            Init();
            for(int i = 0 ;i < n;i++){
            for(int j = 0 ;j < n;j++){
              read(v);
              rod[cnt].st = i;
              rod[cnt].ed = j;
              rod[cnt++].v = v;
            }
        }
        int q;
        read(q);
        sort(rod,rod + cnt);
        while(q--){
          read2(a,b);
          join(a-1,b-1);
        }
            kruskal();
            print(ans);
        return 0;
    }
     
  • 相关阅读:
    NXOpen 创建方体block代码
    Mysql---2 DDL DML DQL DCL
    Mysql数据库--1数据库操作
    FFmpeg
    Servlet Web
    Java web Springboot
    Java 网络
    Java 注解 反射
    Java 线程状态
    Java Lambda
  • 原文地址:https://www.cnblogs.com/llke/p/10780116.html
Copyright © 2011-2022 走看看