Constructing Roads

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

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

思路:求最小生成树,prim 算法,注意将已经修过的路的权值置为0。

 #include<stdio.h>
int map[][],p[],dist[];
int main()
{
int i,j,k,n,m,a,b,min,len;
while(~scanf("%d",&n))
{
for(i = ;i < n;i ++)
{
for(j = ;j < n;j ++)
scanf("%d",&map[i][j]);
}
scanf("%d",&m);
for(i = ;i < m;i ++)
{
scanf("%d%d",&a,&b);
map[a-][b-] = map[b-][a-] = ;
}
for(i = ;i < n;i ++)
{
p[i] = ;
dist[i] = map[][i];
}
len = dist[] = ;
p[] = ;
for(i = ;i < n;i ++)
{
min = ;
k = ;
for(j = ;j < n;j ++)
{
if(!p[j]&&dist[j]<min)
{
min = dist[j];
k = j;
}
}
len += min;
p[k] = ;
for(j = ;j < n;j ++)
{
if(!p[j]&&dist[j]>map[k][j])
dist[j] = map[k][j];
}
}
printf("%d\n",len);
}
return ;
}

Constructing Roads的更多相关文章

  1. Constructing Roads——F

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

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

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

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

  4. HDU 1102 Constructing Roads

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

  5. Constructing Roads (MST)

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

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

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

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

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

  9. hdu 1102 Constructing Roads Kruscal

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

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

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

随机推荐

  1. io流之写文件

    用Java程序写文件有多种方式,对于不同类型的数据,有不同的写方法.写文件的关键技术点如下: 1.FileOutputStream打开文件输出流,通过write方法以字节为单位写文件,是写文件最通用的 ...

  2. 16_MyBatis中期小结

    [MyBatis是什么] MyBatis是一个持久层框架,Mybatis是一个不完全的ORM框架,SQL语句需要程序员自己去编写,但是MyBatis也有映射(输入参数映射.输出结果映射). MyBat ...

  3. eclipse下使用java调用weka(转)

    原文链接:http://blog.csdn.net/felomeng/article/details/4688257 weka是很好用的机器学习库,这里就不详细介绍了. 言归正传,要使用程序方式使用w ...

  4. JPA的泛型DAO设计及使用

    使用如Hibernate或者JPA作为持久化的解决方案时,设计一个泛型的DAO抽象父类可以方便各个实体的通用CRUD操作.由于此时大部分实体DAO的CRUD操作基本一样,采用泛型设计解决这个问题,带来 ...

  5. 判断JS对象是否拥有某属性的方法举例

    判断JS对象是否拥有某属性 JS是否拥有某属性的判断方法,这里提供两种方式,供大家参考. 1.in 运算符 var obj = {name:'jack'}; alert('name' in obj); ...

  6. 『奇葩问题集锦』Ruby 切换淘宝源报错WARNING: Error fetching data: SSL_connect returned=1 errno=0 state=SSLv3 read s erver certificate B: certificate verify failed

    ===>首先需要使用https<===https://ruby.taobao.org/ 第一步 下载http://pan.baidu.com/s/1kU0rxtH 复制到ruby安装的根目 ...

  7. [转] 使用maven运行java main的三种方式

    原文地址: http://blog.csdn.net/qbg19881206/article/details/19850857?utm_source=tuicool&utm_medium=re ...

  8. thinkphp分页格式的完全自定义,直接输入数字go到输入数字页

    实现分页效果如下: 以下标注红色字体的为重点   找到文件page.class.php在ThinkPHP/Library/Thinkpage.class.php并打开文件,复制函数show,在本文件中 ...

  9. TDirectory.IsRelativePath是否相对路径

    使用函数: System.IOUtils.TDirectory.IsRelativePath class function IsRelativePath(const Path: string): Bo ...

  10. HDU1004 (数组元素出现最多)

    HDU1004 思路:求数组中出现次数最多的那个元素: 遍历数组元素,找出每个元素出现的次数 Input Input contains multiple test cases. Each test c ...