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行中,会给出修好 ...
随机推荐
- Unity3d截图保存到Android相册的实现
Unity3d截图保存到Android相册的实现-----------------------------ultrasoon 季风原创--------------------------------- ...
- cocos2d-x游戏是怎么跑起来的
虽然cocos2d-x v3.0 alpha版已经出来了,也改进了不少,有兴趣的可以去尝尝鲜.因为后面可能还会配合cocoStudio写一下博客,而现在v1.0.0.0版本需要配合cocos2d-x ...
- Scala初体验
因为工作中要用到Scala了,本来前面自己还在学习Storm的,没有办法,先把Scala和Spark的这些内容学完在回去看Storm吧! 既然我们要学习Scala,那么我们不禁的要问了,什么是Scal ...
- LeetCode57 Insert Interval
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- Comparing Your Heros拓扑序列的数量
给出N行英雄的比较,每一行包含两个英雄的名字,代表第一个英雄比第二个英雄更受欢迎. 英雄的数目不超过16个.问有多少种可能的受欢迎程度的序列满足N行英雄的比较. 由于只有英雄数目不超过16个,可以用二 ...
- 【阿里云产品公测】PTS测试 SLB+ECS+RDS组合的DZ论坛负载极限压力,100并发2000页
作者:阿里云用户woaj01 环境介绍: 1.ECS:1核 1G 5M 杭州 2.RDS:240M 5G 杭州内网 3.SLB:私网实例 配置测试环境: 测试脚本: 1.生成参数文件,我的方 ...
- 用Eclipse插件Bytecode Outline来查看Java字节码
在遇到一些小问题的时候我们经常会使用Javap反编译取得字节码来分析,虽然Javap能完成这个工作,但是有两个缺点,一方面操作麻烦,需要很多步骤,一方面没有文档注释,对新手来说看起字节码来比较麻烦. ...
- 新浪SAE部署django博客
步骤: 第一步:注册新浪SAE账号(即新浪微博),下载TortoiseSVN 第二步:部署代码 使用SAE来部署代码,SAE提供的是PAAS层的云服务,即不是给你一个虚拟主机而是直接上传应用.进入SA ...
- Learn Vim
Vim Note 很早就知道vim是一个很强大的编辑器,也用了很久.不过没有系统的总结过,这次就写个笔记方便以后看看(本文在vim下编辑完成) 第一印象 打开vim第一感觉就是无从下手,相信大多数人和 ...
- 2014年互联网IT待遇【转载】
2014年互联网IT待遇[转载] 一.民企 1.百度 13k*14.6,special 14~17k*14.6 开发类 13K*14.6 (2014) 测试类.前端类 12K*14.6 ( ...