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 ...
随机推荐
- 每周分享之cookie详解
本章从JS方向讲解cookie的使用.(实质上后端代码也是差不多用法,无非读取和设置两块) 基本用法:document.cookie="username=pengpeng"; 修改 ...
- VMware威睿
VMware总部位于美国加州帕洛阿尔托 [1] ,是全球云基础架构和移动商务解决方案厂商,提供基于VMware的解决方案, 企业通过数据中心改造和公有云整合业务,借助企业安全转型维系客户信任 [2- ...
- CodeIgniter框架对数据库查询结果进行统计
假设有一个user表,如果要查询符合条件sex=male的记录数量,有下面几种方法: 方法一:先取回所有符合条件的记录,再count $res = $this->db->query(&qu ...
- oracle服务端安装与配置
从oracle官网下载oracle服务端的安装包. 下载下来是两个压缩文件,两个压缩文件都解压(缺一不可)到同一目录下,最后会得到一个database文件夹. 双击database文件夹下的setup ...
- Oracle 创建外部表
Oracle 外部表能迅速的将海量的数据导入到数据库里面,外部表的创建使用步骤如下: 1 创建一个Directory:必须用sys用户创建,用户存放外部数据文件. create directory D ...
- 【Java基础】求出1-100之间偶数和
结果:
- js如何复制一个对象?
方法一: 把原来对象的属性遍历一遍,赋给一个新的对象. //深复制对象方法 var cloneObj = function (obj) { var newObj = {}; if (obj insta ...
- 转:win7下git凭据导致无法clone代码
win7下存在一个凭据管理的情况,如果旧凭据没有删除,用新账户是无法clone代码的. https://blog.csdn.net/qq_34665539/article/details/804082 ...
- 简单易懂的softmax交叉熵损失函数求导
参考: https://blog.csdn.net/qian99/article/details/78046329
- NIO服务器与客户端
这里客户端没有采用NIO形式 服务器: package com.util.Server.NIO; import javax.print.DocFlavor;import java.io.IOExcep ...