Constructing Roads

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

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.
 
 
 多的一点就是 这里告诉你了哪些是已经建立好的 模拟一下prime的过程 每次都是把最近的点放进去 然后更新 那么我们把已经建立好点之间的距离设为0 那么这两个点便会优先入
#include<cstdio>
#include<iostream>
#include<string.h>
#include<cmath>
#define maxn 105
#define inf 9999999
int vis[maxn],n;//用来表示点是否在集合里
int mincost[maxn];//用来记录从集合出发到每个点的最小距离
int mapp[maxn][maxn];
using namespace
std;
void
init()
{

for
(int i=;i<=n;i++) vis[i]=;
fill(mincost,mincost+n+,inf);
}

void
prim()
{

mincost[]=;
int
res=;
while
()
{

int
v=-;
for
(int i=;i<=n;i++) if(!vis[i]&&(v==-||mincost[i]<mincost[v])) v=i;//找出离集合最近的点
if(v==-) break;
vis[v]=;
res+=mincost[v];
for
(int i=;i<=n;i++) mincost[i]=min(mincost[i],mapp[v][i]);//放入以后 更新
}

printf("%d\n",res);
}

int
main()
{

while
(~scanf("%d",&n))
{

init();
for
(int i=;i<=n;i++)
{

for
(int j=;j<=n;j++)
{

int
x;
scanf("%d",&x);
mapp[i][j]=mapp[j][i]=x;
}
}

int
q;
scanf("%d",&q);
while
(q--)
{

int
a,b;
scanf("%d %d",&a,&b);
mapp[a][b]=;
mapp[b][a]=;
// vis[a]=1;
// vis[b]=1;
// for(int i=1;i<=n;i++) mincost[i]=minn(mincost[i],mapp[a][i]);//更新集合到每个点的最小距离 v为新点
// for(int i=1;i<=n;i++) mincost[i]=minn(mincost[i],mapp[b][i]);
}
prim();
}

return
;
 
 
 

hdu 1002 prime 模板的更多相关文章

  1. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  2. HDU 2138 Miller-Rabin 模板题

    求素数个数. /** @Date : 2017-09-18 23:05:15 * @FileName: HDU 2138 miller-rabin 模板.cpp * @Platform: Window ...

  3. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  4. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...

  5. HDU 1002 A - A + B Problem II (大数问题)

    原题代号:HDU 1002 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 原题描述: Problem Description I have a ...

  6. hdu 1002 A+B

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1002 复习一下大数 模板: #include <stdio.h> #include <s ...

  7. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  8. hdu 1002.A + B Problem II 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...

  9. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. Linux中强大的top命令

    top命令算是最直观.好用的查看服务器负载的命令了.它实时动态刷新显示服务器状态信息,且可以通过交互式命令自定义显示内容,非常强大. 在终端中输入top,回车后会显示如下内容:   top - 21: ...

  2. sql data compare

    https://documentation.red-gate.com/sdc14 About SQL Data Compare With SQL Data Compare, you can compa ...

  3. Java 内存模型学习笔记

    1.Java类 public class Math { public static final Integer CONSTANT = 666; public int math(){ int a = 1 ...

  4. 二维背包---P1855 榨取kkksc03

    P1855 榨取kkksc03 题解 二维背包板子题 f[ i ][ j ] 前 n 个物品,花费金钱不超过 i ,花费时间不超过 j 的最大价值 如果每个物品只能选一次,那么就相当于在01背包上多加 ...

  5. gis空间分析案例_坐标文件高斯投影变换地理处理工具

    gis空间分析案例_坐标文件投影变换地理处理工具 商务科技合作:向日葵,135—4855__4328,xiexiaokui#qq.com 功能: 对文件进行投影变换 特点: 1. 地理处理工具,可以与 ...

  6. mp4文件格式解析(转)

    mp4文件格式解析 MP4文件格式带数据详解 MP4文件格式的解析,以及MP4文件的分割算法

  7. linux vim基本操作

    vim 是一款功能强大的文本编辑器, 默认有三种模式: 命令模式, 插入模式, 编辑模式, 使用# vim file 打开一个文件时,默认进入命令模式, 不同模式直接的切换如下    a. 命令模式切 ...

  8. 使用弹窗批量修改数据POPUP_GET_VALUES

    转自:https://blog.csdn.net/huanglin6/article/details/81231215 业务场景:在SAP内,有时候需要用户批量维护某些数据,这时候可以使用标准函数PO ...

  9. InfluxDB+Grafana大数据监控系列之数据源配置(二)

    一.Grafana 配置 InfluxDB 数据源 1.1 登录 Granfana 界面选择 InfluxDB 数据源 在前面我们已经部署好相应监控环境,登录Grafana:http://10.223 ...

  10. (三)Centos之安装Xshell

    Xshell就是一个远程控制Centos的软件:(用XShell比较方便,试用的都知道,界面也人性化) 详细介绍请看 百度百科 我随便百度下载了一个中文版的 给下地址  http://pan.baid ...