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

    Constructing Roads
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 23309   Accepted: 9997

    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

    PKU Monthly,kicc
    题目大意时间:给出n(表示点数),n*n的矩阵,表示两点之间的距离,再给出m对数x,y表示x,y本来就连通,求最小生成树
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 int n,m,x,tot,y,fa[1005];
     7 #define maxm 10010
     8 struct node{
     9     int from,to,value; 
    10     bool operator < (const node& a) const {
    11         return value<a.value;
    12     }
    13 }edge[maxm*4];
    14 void add_edge(int u,int v,int w){
    15     edge[++tot].from=u;edge[tot].to=v;edge[tot].value=w;
    16 }
    17 int find(int x){
    18     if(x==fa[x]) return x;
    19     else return fa[x]=find(fa[x]);
    20 }
    21 int main()
    22 {
    23     scanf("%d",&n);
    24     for(int i=1;i<=n;i++)
    25         for(int j=1;j<=n;j++){
    26             scanf("%d",&x);
    27              add_edge(i,j,x);
    28         }
    29     scanf("%d",&m);
    30     for(int i=1;i<=m;i++){
    31         scanf("%d%d",&x,&y);
    32         add_edge(x,y,0);add_edge(y,x,0);
    33     }
    34     sort(edge+1,edge+tot+1);
    35     int t=0,ans=0;
    36     for(int i=1;i<=n+5;i++) fa[i]=i;
    37     for(int k=1;k<=tot;k++){
    38         int x=edge[k].from,y=edge[k].to;
    39         int rx=find(x),ry=find(y);
    40         if(rx!=ry){
    41             ans+=edge[k].value;
    42             fa[rx]=ry;
    43             t++;
    44         }
    45         if(t== n-1 ) break;
    46     }
    47     printf("%d
    ",ans);
    48     return 0;
    49 } 

    思路:还用说嘛!!啊!说一下吧,前面的数据建立边表,后面的数据将从x到y、y到x的距离改成0,跑一边Kruskal

  • 相关阅读:
    2019-07-15_nginx 启动报错 “/var/run/nginx/nginx.pid" failed” 解决方法
    2019-07-12 linux下关闭、开启防火墙
    2019-07-12nginx的安装,启动,关闭
    2019-07-11 nginx 下网页显示乱码
    2019-07-05 submit左对齐快捷键
    2019-07-02 windows下用cmd命令netstat查看系统端口使用情况
    设计一个精致按钮
    JS 实现省市三级联动
    使用style 对象实现图片的覆盖(overlay)
    display 显示或隐藏元素
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6347043.html
Copyright © 2011-2022 走看看