Constructing Roads——F
F. Constructing Roads
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
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
Sample Input
- 3
- 0 990 692
- 990 0 179
- 692 179 0
- 1
- 1 2
Sample Output
- 179
- 题意:
有N个村庄,编号从1到N。现需要在这N个村庄之间修路,使得任何两个村庄之间都可以连通。称A、B两个村庄是连通的,
当且仅当A与B有路直接连接,或者存在村庄C,使得A和C两村庄之间有路连接,且C和B之间有路连接。已知某些村庄之间已经有
路直接连接了,试修建一些路使得所有村庄都是连通的、且修路总长度最短。
- #include <cstdio>
- #include <iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- const int MAXN=;
- int p[MAXN];
- bool sum[MAXN];
- int m[MAXN][MAXN];
- struct node
- {
- int x,y,l;
- }a[];
- bool cmp(node a,node b)
- {
- return a.l<b.l;
- }
- int Find(int x)
- {
- return x==p[x]?x:(p[x]=Find(p[x]));
- }
- int Union(int R1,int R2)
- {
- int r1=Find(R1);
- int r2=Find(R2);
- if(r1!=r2)
- {
- p[r1]=r2;
- return ;
- }
- else return ;
- }
- int main()
- {
- int n;
- int cnt=,i,j;
- while(~scanf("%d",&n))
- {
- cnt =;
- memset(sum,,sizeof(sum));
- for(i=;i<=n;i++)
- p[i]=i;
- for(i=;i<=n;i++)
- for( j=;j<=n;j++)
- scanf("%d",&m[i][j]);
- int t,c,b;
- scanf("%d",&t);
- while(t--) //将已经修好的路长度清零
- {
- scanf("%d%d",&c,&b);
- m[c][b]=m[b][c]=;
- }
- int k=;
- for(i=;i<=n;i++)
- {
- for(j=+i;j<=n;j++)
- {
- a[k].x=i;
- a[k].y=j;
- a[k].l=m[i][j];
- k++;
- }
- }
- sort(a,a+k,cmp);
- for(i=;i<k;i++)
- {
- if(Union(a[i].x,a[i].y)==)
- cnt+=a[i].l;
- }
- printf("%d\n",cnt);
- }
- return ;
- }
Constructing Roads——F的更多相关文章
- hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads (MST)
Constructing Roads Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
随机推荐
- web在线打印,打印阅览,打印维护,打印设计
winform打印的方案比较多,实现也比较容易,而且效果也非常炫:但现在越来越多的系统是web系统,甚至是移动端.网上也有非常的web打印方案,但各式各样的问题非常多,比如js兼容性,稳定性等一直缠绕 ...
- Mongo DB 2.6 需要知道的一些自身限定
在现实的世界中,任何事情都有两面性,在程序的世界中,亦然! 我们不论是在使用一门新的语言,还是一门新的技术,在了解它有多么的让人兴奋,让人轻松,多么的优秀之余,还是很有必要了解一些他的局限性,方便你在 ...
- 2013成都网络赛 J A Bit Fun(水题)
A Bit Fun Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- http_build_query 的一个问题
当我们使用CURL来post数据的时候,需要设置post的数据 curl_setopt($c, CURLOPT_POSTFIELDS, $post_data); 假如这里的$data是 $data = ...
- Linux 定时任务 Crontab命令 详解
前言 crontab是Unix和Linux用于设置周期性被执行的指令,是互联网很常用的技术,很多任务都会设置在crontab循环执行,如果不使用crontab,那么任务就是常驻程序,这对你的程序要求比 ...
- PhpStorm 2016.3 For Mac 重大里程碑更新 -- 终于解决了不能输入中文标点符号的重大bug
PhpStorm 2016.3 For Mac 重大里程碑更新 1.[终于解决了]不能输入中文标点符号的重大bug,如 逗号“,”.“.”: 2.可以在一个窗体中,同时打开多个项目: 3.其他... ...
- 用Canvas写桌球游戏!!!
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 昨天上班的时候闲着无事,就用Canvas写了个桌球游戏来玩玩....所以就拿这游戏上来水一发.或许对一些刚学canvas的人有帮助. 话说 ...
- sdcms标签
模板防盗:<%if not in_sdcms then response.write("template load fail"):response.end() end if% ...
- Java 调用 C++ (Java 调用 dll)康哥手把手教你
摘要: 本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4979243.html 因为要做点图形处理的项目,需要在Java中调用dll库,所以开发的第一步是 ...
- WindowsForm菜单工具栏--2016年12月6日
ContextMenuStrip 添加控件后可在其他空间属性中进行绑定 MenuStrip 设置热键:在编辑的时候输入(&F) 设置快捷键:选中菜单项--右键属性--S ...