Constructing Roads

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23343   Accepted: 10015

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个村庄,从1到N编号,建立一些道路,这样任意两个村庄能连接到对方。我们说两个村庄A和B是连接,当且仅当有A和B之间的道路,或存在C这样的一个村庄之间有一条路A和C,C和B连接。

我们知道,已经有一些道路之间的一些村庄,你的工作是构建道路,所有的村庄都连接和道路建设是最小的长度。

输入描述:

第一行是一个整数N(3 < = N < = 100),这是村庄的数量。然后N行,其中包含N个整数,第i和j N个整数的距离(距离应该是整数在[1000])j村之间我和村庄。

还有一个整数(0 < = Q < = N *(N + 1)/ 2)。然后再问行,每一行包含两个整数a和b(1 < = < b < = N),这意味着路村和村b之间已经建立了。

输出描述:

你应该输出一行包含一个整数,它是所有道路的长度建立连接,这样所有的村庄,这个值是最低。

 /*
很简单的最小生成树
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{int x,y,t;}e[];
inline void read(int &x)
{
int f=;x=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
x=x*f;return ;
}
bool cmp(const node &a,const node &b){return a.t<b.t;}
int n,m,x,y,ans=,fa[],sum=;
int find(int x)
{
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void add(int x,int y)
{
int xx=find(x),yy=find(y);
fa[xx]=yy;
}
int main()
{
read(n);
for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
sum++;
read(e[sum].t);
e[sum].x=i;e[sum].y=j;
}
read(m);
for(int i=;i<=m;i++)
{
read(x),read(y);
e[++sum].x=x;
e[sum].y=y;
e[sum].t=;
}
sort(e+,e+sum+,cmp);
m=;
for(int i=;i<=sum;i++)
{
int xx=find(e[i].x),yy=find(e[i].y);
if(xx!=yy)
{
ans+=e[i].t;
add(xx,yy);
m++;
}
if(m==n-)break;
}
printf("%d\n",ans);
return ;
}

POJ2421Constructing Roads的更多相关文章

  1. POJ-2421-Constructing Roads(最小生成树 普利姆)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26694   Accepted: 11720 Description The ...

  2. POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K               Description There are N v ...

  3. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  4. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  6. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  7. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  8. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  9. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

随机推荐

  1. 温故知新-网络基础1-ip地址计算

    网络地址换算 1.网络地址分类 A类:0.0.0.0到127.255.255.255 255.0.0.0/8 B类:128.0.0.0到191.255.255.255 255.255.0.0/16 C ...

  2. 自建Git服务Gogs和CI/CD服务Drone

    自建Git服务Gogs和CI/CD服务Drone 项目:https://gogs.io Gogs运行 docker run -d --name=gogs -p 10022:22 -p 10088:30 ...

  3. 运行时找到main方法所在的类

    private Class<?> deduceMainApplicationClass() { try { StackTraceElement[] stackTrace = new Run ...

  4. git push proxy 取消不掉 can not prox....

    使用这个折腾了半天 git config --global --unset http.proxy git config --global --unset https.proxy 没用,原来实现项目目录 ...

  5. session 在PC端正常设置读取,在移动端无法正常读取

    一.背景 最近在做一个面向三端[H5.IOS.安卓]的短信验证码登录接口.发送短信验证码时,服务端通过 session 保存验证码的值.登录时,从 session 获取验证码和用户输入的验证码 相比较 ...

  6. grant_type为client_credentials和password二者的区别

    最近工作中需要使用到oauth,注意到oauth客户端的grant_type值可以指定为client_credentials和password两种,很好奇所以网上搜索了一下,发现stackoverfl ...

  7. Arm存储器

    Arm可以引出27根地址线,只能实现128MB的寻址,那么要如何实现1GB的寻址呢?答案就是使用nGCS片选线,nGCSx为低电平为选中相应的外接设备.一共八根片选线,也就是bank1,bank2-以 ...

  8. Android笔记(六十九) 仿微信界面(一)

          综合之前的Fragment和自定义组件的知识,实现微信界面 MainActivity.java package cn.lixyz.test; import android.app.Acti ...

  9. props、state、forms

    {}用来内嵌任何JS表达式JSX属性JS核心分为三大块:Es6.DOM.WindowBABEL编译器:可以在线编译html语法生成对应的react语法 **自定义组件第一个字母大写:用于区别普通的对象 ...

  10. zabbix监控MySQL状态值获取不到值原因分析

    在server端测试键值 [root@zbx-server etc]# zabbix_get -s MySQL-glibc -k "buffer_pool_wait_free" 如 ...