zoukankan      html  css  js  c++  java
  • POJ2421 Constructing Roads

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 23071   Accepted: 9894

    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

     
    给出一个邻接矩阵,再给出Q条已经连好的边,求最小生成树中最大边的距离。
    裸最小生成树。
     
     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<vector>
     8 using namespace std;
     9 const int mxn=3210;
    10 int read(){
    11     int x=0,f=1;char ch=getchar();
    12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    14     return x*f;
    15 }
    16 struct edge{
    17     int x,y;
    18     int d;
    19 }e[mxn*100];
    20 int cmp(const edge a,const edge b){
    21     return a.d<b.d;
    22 }
    23 int m,n;
    24 //
    25 int fa[mxn];
    26 int find(int x){
    27     if(fa[x]==x)return x;
    28     return fa[x]=find(fa[x]);
    29 }
    30 int cnt=0;
    31 void kruskal(){
    32 
    33     int num=1;
    34     int ans=0;
    35     for(int i=1;i<=cnt;++i){
    36         int f1=find(e[i].x);
    37         int f2=find(e[i].y);
    38         if(f1!=f2){
    39             fa[f1]=f2;
    40             ans+=e[i].d;
    41 //            printf("%d %d %d
    ",e[i].x,e[i].y,ans);
    42             num++;
    43         }
    44         if(num==cnt-m-1)break;
    45     }
    46     printf("%d
    ",ans);
    47     return;
    48 }
    49 int T;
    50 int main(){
    51     int i,j;
    52     n=read();
    53     cnt=0;
    54     for(i=1;i<=n;i++)
    55      for(j=1;j<=n;j++){
    56          m=read();
    57          if(i==j)continue;
    58          e[++cnt].x=i;e[cnt].y=j;
    59          e[cnt].d=m;
    60 //         printf("  %d %d %d
    ",i,j,m);
    61      }
    62     sort(e+1,e+cnt+1,cmp);
    63     int u,v;
    64     m=read();
    65     for(int i=1;i<=n;++i)fa[i]=i;
    66     for(i=1;i<=m;++i){
    67         u=read();v=read();
    68         u=find(u);v=find(v);
    69         if(u!=v)fa[u]=v;
    70     }
    71     kruskal();
    72     return 0;
    73 }
  • 相关阅读:
    数据库的优化(非连接查询和连接查询的巧用)
    sql中为表添加一个含有括号的字段
    如何在有int型主键遍历表中的某一列数据
    三层架构的基本例子
    委托和事件
    sql中的常见函数
    博客园图灵杯第3届博问大赛(8.28~9.28)
    程序员部落酋长 Joel 之洞见
    安全领域多位世界级权威的智慧结晶——《黑客新型攻击防范:深入剖析犯罪软件》
    图灵“微软四大技术秘籍”近期出版!
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6017384.html
Copyright © 2011-2022 走看看