Hdoj 1102.Constructing Roads 题解
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
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
Source
思路
就是求最小生成树的问题,已经修过的路就把距离设置为0就好了,用的是Kruskal算法
代码
#include<bits/stdc++.h>
using namespace std;
int father[110];
struct Graph
{
int u;
int v;
int dis;
}maps[10010];
int grid[110][110];
void init(int n)
{
for(int i=1;i<=n;i++) father[i]=i;
}
int find(int x)
{
while(father[x]!=x) x=father[x];
return x;
}
void join(int a,int b)
{
int t1=find(a);
int t2=find(b);
if(t1!=t2) father[t1]=t2;
}
int getNum(int n)
{
int num = 0;
for(int i=1;i<=n;i++)
if(father[i]==i)
num++;
return num;
}
int main()
{
int n;
while(cin>>n)
{
memset(grid,0,sizeof(grid));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin >> grid[i][j];
int q;
cin >> q;
while(q--)
{
int a,b;
cin >> a >> b;
grid[a][b] = 0;
grid[b][a] = 0;
}
int edgeNum = 0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j)continue;
maps[++edgeNum].u = i;
maps[edgeNum].v = j;
maps[edgeNum].dis = grid[i][j];
}
sort(maps+1,maps+1+edgeNum,[](Graph x,Graph y)->bool{ return x.dis<y.dis;});
init(n);
int distance = 0;
for(int i=1;i<=edgeNum;i++)
{
if(find(maps[i].u) != find(maps[i].v))
{
join(maps[i].u,maps[i].v);
distance += maps[i].dis;
}
}
cout << distance << endl;
}
return 0;
}
Hdoj 1102.Constructing Roads 题解的更多相关文章
- hdoj 1102 Constructing Roads
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 分析:看到这题给出的都是矩阵形式,就知道了可以用Prim算法求MST. #include <i ...
- (MST) HDOJ 1102 Constructing Roads
怎么说呢 这题就是个模板题 但是 hud你妹夫啊说好的只有一组数据呢??? 嗯??? wa到家都不认识了好吗 #include <cstdio> #include <cstring& ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 《梦断代码》Scott Rosenberg著(一)
两打程序员,3年时间,4732个bug,只为打造超卓软件. --序 在我们平时看到的大部分书籍只是讲技术和理论,但我们其实并不知道在真实的软件开发过程中,这些技术和理论究竟是被什么样的人如何去使用, ...
- [2017BUAA软工助教]团队开发阶段CheckList
alpha阶段流程与相关节点 以下流程与团队项目中个人的得分点是一一对应的,详见QA文档中"个人在团队项目的得分部分" http://www.cnblogs.com/Childis ...
- Java Core - 序列化和反序列化
把对象转换为字节序列的过程称为对象的序列化 把字节序列恢复成对象的过程称为对象的反序列化 一.对象的序列化的应用: 1.把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中. 2.在网络上传送对象 ...
- 数组建 BST
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int N, root = 1; int ...
- JS 验证输入框输入 只允许输入正实数(正整数,正小数),其他情况下不能输入 oninput事件
input标签的oninput事件 要求输入框只输入正实数,包括整数和小数. 具体要求:整数部分不超过7位,可以没有小数,若有位数不超过2位. <input type="text&qu ...
- vue实现双向数据绑定之Object.defineProperty()篇
前言 vue.js中使用ES5的Object.defineProperty()实现数据的双向绑定 Object.defineProperty()原理 Object.defineProperty()可以 ...
- day 7-19 Mysql索引原理与查询优化
一,介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语 ...
- mac 中登陆mysql忘记密码解决办法
1.打开终端,输入命令:cd /usr/local/mysql/bin 2.mysql -uroot -p,用这条命令登陆时报错信息: 报错:Enter password: ERROR 1045 (2 ...
- 第六周作业----PSP&工作量
1. PSP 日期 类别 工作 开始时间 中断时间 结束时间 总时间 4.7 站立会议 "耐撕"团队站立会议 20:00 20:15 15 重构 重构"抢答器&q ...
- uwsgi加nginx部署django restframework前后端分离项目
一.uwsgi和nginx简介 1.uwsgi(摘抄于百度百科): uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与 ...