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. webpack ,react

    一小时包教会 —— webpack 入门指南 http://***/Article/50764   ------------------ 轻松入门React和Webpack http://www.in ...

  2. 《javascript权威指南》第9章 例9-8源码

    //创建一个新的枚举类型 //不能使用它来创建该类型的新实例 function Enumeration(nameToValues){ var Enumeration = function(){thro ...

  3. linux 搭建lamp环境

    sudo apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql sudo chmod 777 /var/w ...

  4. c#进程间通信(Inter-Process Communication)

    原文:c#进程间通信(Inter-Process Communication) c#进程间通信(IPC, Inter-Process Communication) 接收端: using System; ...

  5. vs2010 suite integration toolkit execution

    原因是UltraDeamen的问题,重新换个WinMount来解压ISO文件.完美安装运行

  6. 2015必须推荐的Android框架,猿必读系列!

    一.Guava Google 的基于java1.6的类库集合的扩展项目,包括collections, caching, primitives support, concurrency librarie ...

  7. 【Android】通过Java代码替换TabHost中的drawableTop资源

    在博客 http://blog.csdn.net/jueblog/article/details/11837445 中的Tab选项卡中, 点击相应的Tab选项,图标没有发生改变. 这些资源图片也没有尽 ...

  8. Xmind 快捷键

    Xmind 快捷键 快捷键(Windows) 快捷键(Mac) 描述 Ctrl+N Command+N 建立新工作簿 Ctrl+O Command+O 开启工作簿 Ctrl+S Command+S 储 ...

  9. Android ToggleButton使用介绍

    ToggleButton,就是开关按钮,包括选中和未选中状态,并且需要为不同的状态设置不同的事件处理: 例如:使用图片来展示ToggleButton不同的状态: MainActivity.java p ...

  10. js 获取input file路径改变图像地址

    html代码 <img id="newImage" alt="100x100" src="__PUBLIC__/img/1.jpg" ...