题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1102

Constructing Roads

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

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
分析:
裸最小生成树
注意点:
自己到自己的距离初始化为无穷大
多组输入(这个题目没有说,但是需要才能ac)
存图的时候要多注意,要细心,不能存错了,尤其是下标
#include<bits/stdc++.h>
using namespace std;
#define INF 1000000
#define max_v 105
int g[max_v][max_v];//g[i][j] 表示i点到j点的距离
int n,sum;
void init()
{
for(int i=; i<n; i++)
for(int j=; j<n; j++)
g[i][j]=INF;
}
void prim()
{
// int close[n];//记录不在s中的点在s中的最近邻接点
int lowcost[n];//记录不在s中的点到s的最短距离,即到最近邻接点的权值
int used[n];//点在s中为1,否则为0
for(int i=; i<n; i++)
{
//初始化,s中只有一个点(0)
lowcost[i]=g[][i];//获取其他点到0点的距离,不相邻的点距离无穷大
// close[i]=0;//初始化所有点的最近邻接点都为0点
used[i]=;//初始化所有点都没有被访问过
}
used[]=;
for(int i=; i<n; i++)
{
//找点
int j=;
for(int k=; k<n; k++) //找到没有用过的且到s距离最小的点
{
if(!used[k]&&lowcost[k]<lowcost[j])
j=k;
}
// printf("%d %d %d\n",close[j]+1,j+1,lowcost[j]);
sum+=lowcost[j];
used[j]=;//j点加入到s中
//松弛
for(int k=; k<n; k++)
{
if(!used[k]&&g[j][k]<lowcost[k])
{
lowcost[k]=g[j][k];
// close[k]=j;
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
sum=;
init();
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
int x;
scanf("%d",&x);
if(i==j)
continue;
g[i][j]=x;
}
}
int q;
scanf("%d",&q);
for(int i=; i<q; i++)
{
int a,b;
scanf("%d %d",&a,&b);
g[a-][b-]=;
g[b-][a-]=;
}
prim();
printf("%d\n",sum);
}
return ;
}

HDU 1102(Constructing Roads)(最小生成树之prim算法)的更多相关文章

  1. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  2. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  3. (step6.1.4)hdu 1102(Constructing Roads——最小生成树)

    题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...

  4. HDU 1102 Constructing Roads(最小生成树,基础题)

    注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...

  5. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  6. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  7. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  8. hdu 1102 Constructing Roads(kruskal || prim)

    求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...

  9. hdu 1102 Constructing Roads Kruscal

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

随机推荐

  1. 洛谷P3939 数颜色(二分 vector)

    题意 题目链接 Sol 直接拿vector维护每种颜色的出现位置,然后二分一下. #include<bits/stdc++.h> using namespace std; const in ...

  2. 探索canvas画布绘制技术

    图片来自KrzysztofBanaś 下面我们开始尝试研究不同的绘图风格和技术 - 边缘平滑,贝塞尔曲线,墨水和粉笔,笔和印章和图案 -等等.事实证明,网上没有太多关于此的内容.在下面的示例中,您请大 ...

  3. CSS知多少

    1.Cascading Style Sheets 层叠样式表 2.层叠就是浏览器对多个样式来源进行叠加,最终确定结果的过程. 3. 样式的5大来源:浏览器默认样式.浏览器用户自定义样式.行内样式.内部 ...

  4. js图片跟随鼠标移动

    <div id="wrapper"><img src="http://images.cnblogs.com/cnblogs_com/rain-null/ ...

  5. 什么是J2EE

    什么是J2EE 一.准备篇 1 什么是J2EE?它和普通的Java有什么不同? 答:J2EE全称为Java2 Platform Enterprise Edition. "J2EE平台本质上是 ...

  6. Appium+java移动端项目测试问题整理

    一.每次打开APP都要重新安装.充值账号密码 解决:打开appium,设备,Use Browser  ,勾选“No Reset”   二.一个页面包含相同文字,打开页面路径错误 问题描述:APP处于[ ...

  7. MyBatis基本配置和实践(四)

    一.Mybatis整合spring 1.整合思路 SqlSessionFactory对象应该放到spring容器中作为单例存在. 传统dao的开发方式中,应该从spring容器中获得sqlsessio ...

  8. androidcookie存储sqllite

    /**声明一些数据库操作的常量*/  private static SQLiteDatabase mDatabase = null;  private static final String DATA ...

  9. python中的内容编码

    一.python编码简介 1)编码格式简介 python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ASCII),ASCII(American Standard Code for In ...

  10. Python学习---面向对象的学习[深入]

    类的深入学习    a. Python中一切事物都是对象     b. class Foo:             pass                obj = Foo()         # ...