Constructing Roads

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

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
 

最小生成树....

代码:

 //最小生成树....
//@Gxjun coder
#include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f ,v=;
int vis[v],lowc[v];
int sta[v][v];
int prim(int cost[][v],int n)
{
int i,j,p;
int minc,res=;
memset(vis , , sizeof(vis));
vis[]=;
for(i= ; i<n ;i++)
lowc[i]=cost[][i];
for(i=;i<n;i++)
{
minc=inf;
p=-;
for(j= ; j<n ;j++)
{
if(==vis[j] && minc>lowc[j])
{
minc=lowc[j];
p=j;
}
}
if(inf==minc) return -; //原图不连通
res+=minc ;
vis[p]=;
for(j=; j<n ;j++)
{
if(==vis[j] && lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
}
return res;
} int main()
{
int tol,res,i,j;
while(scanf("%d",&tol)!=EOF)
{
for(i=;i<tol;i++)
{
for(j=; j<tol;j++)
{
scanf("%d",&sta[i][j]);
}
}
scanf("%d",&res);
int num1,num2;
for(i=;i<res;i++)
{
scanf("%d%d",&num1,&num2);
sta[num1-][num2-]=sta[num2-][num1-]=;
}
printf("%d\n",prim(sta,tol));
}
return ;
}

HDUOJ---1102Constructing Roads的更多相关文章

  1. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  2. Jungle Roads[HDU1301]

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

  3. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  4. Constructing Roads——F

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

  5. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  6. 杭电--1102--Constructing Roads--并查集

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

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

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

  8. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  9. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  10. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

随机推荐

  1. Latex:表格制作全攻略

    给出一个制作复杂表格的例子,制作表格主要用到multicolumn,multirow和cline,其中,要使用multirow,必须usepackage{multirow} 如果要制作出如下图所示的表 ...

  2. ubuntu 安装 mongodb 数据库

    第一步:下载安装包 下载版本:3.0.1 下载链接:http://www.mongodb.org/downloads 首先在linux中解压缩安装程序 通过命令操作: 解压:[root@localho ...

  3. 我所遭遇过的游戏中间件--Scaleform

    我所遭遇过的游戏中间件---Scaleform Scaleform帮助开发人员利用现代系统的三维硬件加速性能创建电影品质的菜单.游戏内HUD,动画纹理.迷你游戏以及移动游戏与应用.Scaleform作 ...

  4. C语言编译器不检查数组下标越界

    这两天被人问了一个问题说假如C/C++访问下表越界的数组元素会报错么,于是充满好奇心的我动手试了一下,WTF,果然没有报错,但是会给程序带来莫名其妙的结果(比如十次的循环但是变成了死循环,但八次却可以 ...

  5. multiple definition of 问题解决方法

    在最近的项目里,由于我想重载结构体的==操作符,然而大意的我把重载的过程写在了头文件里,所以导致了multiple definition of的错误.现在总结下解决方法: 首先,最关键的,不要把全局变 ...

  6. Visual Studio 2015官方社区版/专业版/专业版下载地址

    Visual Studio 2015官方社区版/专业版/专业版下载地址 以下 Visual Studio 2015 社区版/专业版/专业版资源都是官方MSDN原版下载资源,统一为ISO格式镜像,使用解 ...

  7. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  8. 服务 Service 清单文件中可设置的属性

    PS:对于一个Service,在没有在AndroidManifest.xml中声明的情况下使用时,不会像Activity那样直接崩溃并提示找不到Activity. 对于显式Intent启动的Servi ...

  9. scala 学习笔记五 foreach, map, reduce

    例子 val v = Vector(,,,) ) println(s) //输出:Vector(2, 4, 6, 8) val v2 = Vector(,,,) var v3 = v2.reduce( ...

  10. json字符串和dict互转

    json字符串和dict互转 import json str = '{"params":[{"id":222,"offset":0},{&q ...