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. fastjson框架如何处理boolean?CURRENT_TIMESTAMP使用报错?什么是 ONLINE DDL 及 pt-online-schema-change ? getBytes引起的乱码问题?

    一.使用fastjson框架进行序列化时,若莫个参数为Boolean类型,而json里的值是其它类型时,框架如何处理? 1.true, false,正常赋值2.int类型,若为1,则为true,否则为 ...

  2. 在centos7.6上部署.netcore 3.0 web程序

    首先需要一个全新的centos系统. 第一步:按照微软官方文档配置.netcore环境: https://dotnet.microsoft.com/download/linux-package-man ...

  3. 12.1 Mapping手动创建

    只能在index里的field不存在的时候,才能指定新field的数据类型,field有数据后,就不能再修改field的类型了 可创建的类型如下: integer double date text/s ...

  4. BUAAOO-Third-Summary

    目录 从DBC到JML SMT solver 使用 JML toolchain的可视化输出 和我的测试结果 规格的完善策略 架构设计 debug情况 心得体会 一.从DBC到JML 契约式设计(Des ...

  5. Django:必会ORM查

    1.ORM常用字段 在models.py,创建表中字段 1.1AutoField 自增的整形字段,必填参数primary_key=True,则成为数据库的主键.无该字段时,django自动创建.一个m ...

  6. SIM7000C TCP

    SIM7000C是基于高通MDM9206平台开发LTE CAT M1(eMTC) 和NB-IoT模块,能支持LTETDD/LTE-FDD/GSM/GPRS/EDGE多个频段,上下行数据流量峰值达到37 ...

  7. 过滤器+用session验证是否登陆过

    过滤器: public class MyActionFilter : ActionFilterAttribute//继承ActionFilterAttribute类 { public override ...

  8. 基于NFS的PV动态供给(StorageClass)

    一.简介 PersistentVolume(PV)是指由集群管理员配置提供的某存储系统上的段存储空间,它是对底层共享存储的抽象,将共享存储作为种可由用户申请使的资源,实现了“存储消费”机制.通过存储插 ...

  9. CasPHP 便捷简单的 ThinkPHP 框架

    便捷简单的 PHP 框架,自适应后台管理界面 Convenient and succinct(便捷的 简明的;言简意赅的) 核心框架:framework: 5.1.28助手函数:think-helpe ...

  10. macOS 10.13允许任何来源开启方法

    软件下载网站: http://www.pc6.com/ 软件安装问题: macOS 10.13允许任何来源开启方法: 如果需要恢复允许“任何来源”的选项,即关闭系统的Gatekeeper,我们可以在“ ...