Constructing Roads(最小生成树)
Constructing Roads |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 274 Accepted Submission(s): 172 |
Problem 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 |
Sample Output
179 |
Source
kicc
|
Recommend
Eddy
|
#include<bits/stdc++.h>
#define N 110
using namespace std;
struct node
{
int u,v,val;
node(){}
node(int a,int b,int c)
{
u=a;
v=b;
val=c;
}
bool operator <(const node &a) const
{
return val<a.val;
}
};
vector<node>edge;
int n;
int bin[N];
int findx(int x)
{
while(x!=bin[x])
{
x=bin[x];
}
return x;
}
void init()
{
for(int i=;i<=n;i++)
bin[i]=i;
}
int val;
int x,y,t;
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
init();
edge.clear();
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&val);
if(i>j)
edge.push_back(node(i,j,val));
}
}
sort(edge.begin(),edge.end());
long long cur=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&x,&y);
bin[findx(y)]=findx(x);
}
for(int i=;i<edge.size();i++)
{
int fx=findx(edge[i].u);
int fy=findx(edge[i].v);
if(fx!=fy)
{
bin[fy]=fx;
cur+=edge[i].val;
//cout<<cur<<endl;
}
}
printf("%lld\n",cur);
}
return ;
}
Constructing Roads(最小生成树)的更多相关文章
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- (step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...
- HDU1102 Constructing Roads —— 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...
- POJ2421 Constructing Roads 最小生成树
修路 时限: 2000MS 内存限制: 65536K 提交总数: 31810 接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...
- HDU 1102 Constructing Roads(最小生成树,基础题)
注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
随机推荐
- Zabbix通过SNMP监控多核CPU使用率时, 计算CPU平均使用率
环境:没有Agent,只能通过SNMP监控时,需要获取多核CPU的平均使用率. ZABBIX的使用SNMP监控CPU使用率时,由于设备都是多核CPU,监控的都是单独某一核心的使用率,但单独某一核使用率 ...
- linux环境下根据文件的某一列进行去重
momo@ubuntu:~/user/txt$ cat test.txt1 chen nihao2 chen hello3 li nimenhao4 he dajiahao5 li nimenhaom ...
- Ubuntu 安装 SQL Server
SQL Server现在可以在Linux上运行了!正如微软CEO Satya Nadella说的,"Microsoft Loves Linux",既Windows 10内置的Lin ...
- [SDOI2011]工作安排
Description 你的公司接到了一批订单.订单要求你的公司提供n类产品,产品被编号为1~n,其中第i类产品共需要Ci件.公司共有m名员工,员工被编号为1~m员工能够制造的产品种类有所区别.一件产 ...
- Ubuntu中MongoDB安装
在Ubuntu中MongoDB有时候启动不起来,可以参考以下方法从新安装: 1.导入包管理系统使用的公钥 Ubuntu 的软件包管理工具(即dpkg和APT)要求软件包的发布者通过GPG密钥签名来确保 ...
- 关于Java里面File类创建txt文件重复???
private JButton getOpenButton() { if (openButton == null) { openButton = new JButton(); openButton.s ...
- JS类继承常用方式发展史
JS类继承常用方式发展史 涉及知识点 构造函数方式继承 1-继承单个对象 1.1 多步走初始版 1.2 多步走优化版 1.3 Object.create()方式 2-继承多个对象 2.1 遍历 Obj ...
- 【DDD】业务建模实践 —— 人关注人
社区业务领域中,存在‘人关注人’的场景,在这个场景中,关系较为复杂,且均表现在‘人’同一个业务实体上,因此,这个case的建模过程值得思考.本文将就‘人关注人’这个业务case的领域建模进行探讨,欢迎 ...
- 使用IGP和BGP的配合达到降低路由容量目的的实验与总结
概述 1.先说结论,通过eBGP协议,可以显著降低对非核心路由器的路由容量要求,因为核心路由器的数量明显少于非核心路由器,所以,通过此措施即联通网络,又降低设备要求,非常适宜大型网络. 2.因为网络规 ...
- jquery系列教程3-DOM操作全解
全栈工程师开发手册 (作者:栾鹏) 快捷链接: jquery系列教程1-选择器全解 jquery系列教程2-style样式操作全解 jquery系列教程3-DOM操作全解 jquery系列教程4-事件 ...