Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17187    Accepted Submission(s): 6526

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
 
 
 
 
 
看了好久才看懂,前三行的意思是,第1,2,3个村子分别距离第1,2,3个村子的距离,
 
 
最小生成树来写!
 
 
 
 
 
 
 
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int per[],map[][];
struct node
{
int b,e,w;
}s[];
bool cmp(node x,node y)
{
return x.w<y.w;
}
void init()
{
for(int i=;i<;i++)
per[i]=i;
} int find(int x)
{
while(x!=per[x])
x=per[x];
return x;
} bool join (int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
return true;
}
return false;
}
int main()
{
int n,i,n1,a,b,j;
while(scanf("%d",&n)!=EOF)
{
init();
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&map[i][j]);
scanf("%d",&n1);
for(i=;i<n1;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=;
}
int k=;
for(i=;i<=n;i++)
{
for(j=i;j<=n;j++)
{
s[k].b=i;
s[k].e=j;
s[k].w=map[i][j];
k++;
}
}
sort(s,s+k,cmp);
int sum=;
for(i=;i<k;i++)
{
if(join(s[i].b,s[i].e))
sum+=s[i].w;
}
printf("%d\n",sum);
}
return ;
}

Constructing Roads--hdu1102的更多相关文章

  1. HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏

    Constructing Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  2. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  3. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  4. [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 ...

  5. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. Constructing Roads (MST)

    Constructing Roads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  7. 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 ...

  8. 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 ...

  9. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  10. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

随机推荐

  1. 关于uC/OS的简单学习(转)

    1.微内核 与Linux的首要区别是,它是一个微内核,内核所实现的功能非常简单,主要包括: 一些通用函数,如TaskCreate(),OSMutexPend(),OSQPost()等. 中断处理函数, ...

  2. 通过设计让APP变快的6个方法

    我们都知道不管网页还是移动应用,响应速度都是最重要的体验指标之一,并且移动应用的网络环境不稳定,速度的体验显得尤为重要.其实速度优化不仅是程序员的事,设计,也能够让APP变得更快. 1. 后台执行 这 ...

  3. Qt浅谈之二十七进程间通信之QtDBus

    一.简介 DBus的出现,使得Linux进程间通信更加便捷,不仅可以和用户空间应用程序进行通信,而且还可以和内核的程序进行通信,DBus使得Linux变得更加智能,更加具有交互性.        DB ...

  4. Visual Studio 启动加速

    Who is locking my SQL database?|Deploy a database project with TFS Build Visual Studio 2012 running ...

  5. linux 命令后台执行

    我想把updatedb命令(用于重新建立整盘索引的命令)放在后台运行.因为我不想眼睁睁的看着机器建立索引,我还想编会儿程序呢: # updatedb & [1] 23336 注释:在所要执行的 ...

  6. Android String 转 MD5

    /** * 将字符串转成16 位MD5值 *  * @param string * @return */ public static String MD5(String string) { byte[ ...

  7. 在CentOS/RHEL 6.5上安装Chromium 谷歌浏览器

    之前Google就说了,由于CentOS/RHEL 6已经是过期的系统,所以不再会有Chrome了. 虽然后来由于引起了社区的抗议,从而改口,不再提CentOS/RHEL 6是过期系统了:但是,目前在 ...

  8. JavaScript Function 函数深入总结

    整理了JavaScript中函数Function的各种,感觉函数就是一大对象啊,各种知识点都能牵扯进来,不单单是 Function 这个本身原生的引用类型的各种用法,还包含执行环境,作用域,闭包,上下 ...

  9. DHTML【8】--CSS

    在讲HTML时说过,有个Style标签是在CSS里用的,是的,在HTML中添加CSS样式必须要用到Style,在标签里单独定义标签属性时用的是Style属性.上一节我们也说过,先不用管那个Style标 ...

  10. MySQL server version for the right syntax to use near &#39;type=InnoDB&#39; at line 1

    转载请注明出处:http://blog.csdn.net/bettarwang/article/details/40180271 在执行一个Hibernate的演示样例时,配置了<propert ...