hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102
题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更适合用Prim来做。 用Kruscal的话要做一些变化。
/*Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14983 Accepted Submission(s): 5715 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
kicc
*/
//Kruscal
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = + ;
int u[maxn], v[maxn], r[maxn], w[maxn], p[maxn], n, q, m, map[ + ][ + ]; void init()
{
memset(u, , sizeof(u)); memset(v, , sizeof(v)); memset(w, , sizeof(w));
memset(p, , sizeof(p)); memset(r, , sizeof(r)); memset(map, , sizeof(map));
} int cmp(const int i, const int j)
{
return w[i] < w[j];
} int find(int x)
{
return x == p[x] ? x : p[x] = find(p[x]);
} int Kruscal()
{
int ans = ;
for(int i = ; i <= n; i++) p[i] = i;
for(int i = ; i <= m; i++) r[i] = i;
sort(r, r+m, cmp);
for(int i = ; i < m; i++){
int e = r[i];
int x = find(u[e]), y = find(v[e]);
if(x != y){
ans += w[e];
p[x] = y;
}
}
return ans;
} int main()
{
int a, b;
while(~scanf("%d", &n)){
init();
m = n*(n-)/;
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d", &map[i][j]);
int k = ;
scanf("%d", &q);
for(int i = ; i < q; i++){
scanf("%d%d", &a, &b);
map[a][b] = map[b][a] = ;
}
for(int i = ; i <= n; i++)
for(int j = i+; j <= n; j++){
u[k] = i; v[k] = j; w[k] = map[i][j];
k++;
}
int cnt = Kruscal();
printf("%d\n", cnt);
}
return ;
}
hdu 1102 Constructing Roads Kruscal的更多相关文章
- 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 (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1102 Constructing Roads(kruskal)
Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...
- 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行中,会给出修好 ...
随机推荐
- 关于去除Dialog的黑色背景框
Dialog有两种形式的,一个是Dialog及其子类,还有一种是Activity的Dialog显示方式. 不管怎样,在自定义Dialog的时候,如果不做一些处理,都会出现黑色背景边框,这个问题动不动就 ...
- iOS开发——网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据
网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...
- 线性表 及Java实现 顺序表、链表、栈、队列
数据结构与算法是程序设计的两大基础,大型的IT企业面试时也会出数据结构和算法的题目, 它可以说明你是否有良好的逻辑思维,如果你具备良好的逻辑思维,即使技术存在某些缺陷,面试公司也会认为你很有培养价值, ...
- D2 前端技术论坛总结(下)
此篇文章不接上篇了,下午4场我就不一一介绍了,主要总结下 D2 整场下来都讲了些什么. 整场下来,就3个关键词:nodejs,多终端,工程化 nodejs 从杭js到d2,大会上提到最多的词汇 ...
- 如何在Linux下拷贝一个目录呢
cp -af newadmin/movie/. uploadfile/mallvideo/ 如何在Linux下拷贝一个目录呢?这好像是再简单不过的问题了. 比如要把/home/usera拷贝到/m ...
- A SQLite client library written in Modern C++
smartdb是一个纯c++11开发,header-only,简洁高效的sqlite封装库. github地址:https://github.com/chxuan/smartdb,如果您觉得不错,请不 ...
- jQuery ajax - ajax() 方法
1.jsp页面 function onSaveClick(btn) {//保存 $.ajax({ url : "" , type : "POST", data ...
- CAKeyframeAnimation 旋转动画
- USB HID 协议入门
转载请注明来源:cuixiaolei的技术博客 USB HID设备类的应用场合 USB HID类是USB设备的一个标准设备类,包括的设备非常多.HID类设备定义它属于人机交互操作的设备,用于控制计算机 ...
- 【Amazon Linux】免费搭建subversion服务器
Amazon的EC2服务器可以免费试用一年.在这里申请: https://aws.amazon.com/cn/free/ 尝试把它弄成一个svn库来保存代码.按照 http://northwaygam ...