Constructing Roads

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

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
昨天没写博客,今天开始练习图论算法
题意:给出每两个村庄之间修路所需的费用,然后有一些村庄的路已经修好了,每两个村庄之间有且仅有一条路,问修这些路的最小花费.
题解:因为题目点少,直接开的邻接矩阵了,先把连通的村庄的路初始化为0 ,然后利用prim算法求解.
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; const int N = ;
const int INF = ;
int graph[N][N];
int low[N];
bool vis[N]; int prim(int n,int start){
memset(vis,false,sizeof(vis));
memset(low,false,sizeof(low));
int pos = start,cost=;
vis[pos]=true;
for(int i=;i<=n;i++){
low[i] = graph[pos][i];
}
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]<Min){
pos = j,Min = low[j];
}
}
vis[pos]=true;
cost+=Min;
for(int j=;j<=n;j++){
if(!vis[j]&&low[j]>graph[pos][j]) low[j] = graph[pos][j];
}
}
return cost;
} int main(){
int n,m;
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
}
}
scanf("%d",&m);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
graph[a][b]=graph[b][a] = ;
}
printf("%d\n",prim(n,));
}
}

hdu 1102(最小生成树)的更多相关文章

  1. HDU 1102 最小生成树裸题,kruskal,prim

    1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...

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

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

  3. 图论问题(2) : hdu 1102

    题目转自hdu 1102,题目传送门 题目大意: 输入一个n*n的邻接矩阵,其中i行j列代表从i到j的路径的长度 然后又m条路已经帮你修好了,求最短要修多长的路才能使所有村庄连接 不难看出,这道题就是 ...

  4. HDU 1233(最小生成树)

    HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...

  5. HDU 1102(Constructing Roads)(最小生成树之prim算法)

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

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

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

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

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

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

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

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

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

随机推荐

  1. layout焊盘过孔大小的设计标准

    PCB设计前准备 1.准确无误的原理图.包括完整的原理图文件和网表,带有元件编码的正式的BOM.原理图中所有器件的PCB封装(对于封装库中没有的元件,硬件工程师应提供datasheet或者实物,并指定 ...

  2. build dynamic libraries for iOS and load them at runtime

    编译了libmt.dylib, 和 test 程序调用,均正常.在xcode中显示调用正常,隐式调用则出现问题. 提示 dyld: Library not loaded. 即使存在在/usr/lib/ ...

  3. 【Hazard of Overfitting】林轩田机器学习基石

    首先明确了什么是Overfitting 随后,用开车的例子给出了Overfitting的出现原因 出现原因有三个: (1)dvc太高,模型过于复杂(开车开太快) (2)data中噪声太大(路面太颠簸) ...

  4. CocosCreator设置模拟器默认横竖屏以及机型

    之前好好的横屏,今天不知道为毛突然变成竖屏了,虽然可以在点击模拟器左上角进行设置,   但是 每次启动模拟器又变成竖屏了,折腾了很久,终于找到了设置的地方,记录下:        

  5. 自动化测试学习之路--HTML常见元素、属性的简单学习

    如何创建html文件: 使用工具:VSCode 1.双击文件名显示区,可快速新建文件. 2.保存文件,文件名.html 3.输入!(必须是英文的!),按 Tab键,可自动生成html格式的文件,如下: ...

  6. Python面试题之一:解密

    Python面试题之一: 说明:就是Python工程师面试题 一.字典转换与正则提取值 1:key与Value交换 a = {'a':1,'b':2} print({value:key for key ...

  7. 一个初学者的辛酸路程-继续Django

    问题1:HTTP请求过来会先到Django的那个地方? 先到urls.py  ,里面写的是对应关系,1个URL对应1个函数名. 如果发URL请求过来,到达这里,然后帮你去执行指定的函数,函数要做哪些事 ...

  8. [译]11-spring bean定义的继承

    spring中bean的定义包含很多信息,如,构造器参数.property指定的依赖项.初始化方法.工厂类和工厂方法等. 如果spring容器的中每个bean都重复声明这些属性,是非常烦人也是十分低效 ...

  9. Day4 自定义控件/ListView/RecyclerView

    创建自定义控件 引入布局 在新增的title.xml中创建一个自定义的标题栏: <LinearLayout xmlns:android="http://schemas.android. ...

  10. Android记事本05

    昨天: intentFilter 今天: URL和logcat 问题: ADK更新后无法打开布局文件.xml