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 }
  • 相关阅读:
    Uliweb模板分析
    从高宏伟处借来的HOOK API代码
    ActiveX获取脚本对象
    uliweb数据库分析
    Java初入门(from c++ 2 java)(一)
    VS2010调试sharepoint2010经常Web服务器进程由IIS终止 的解决办法
    SharePoint 2010 部署 WSP 包
    sharepoint 2010 "若要在 Visual Studio 中与 SharePoint 项目进行交互,您的系统用户帐户必须拥有管理员特权。"的解决方法
    SQL 2008 安装问题 共享功能目录(x86) 服务提供的凭据无效 解决版本
    SharePoint 2010 网站集 备份 还原
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6017384.html
Copyright © 2011-2022 走看看