hdu 1002 prime 模板
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20927 Accepted Submission(s): 8023
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.
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.
#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 模板的更多相关文章
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- HDU 2138 Miller-Rabin 模板题
求素数个数. /** @Date : 2017-09-18 23:05:15 * @FileName: HDU 2138 miller-rabin 模板.cpp * @Platform: Window ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...
- HDU 1002 A - A + B Problem II (大数问题)
原题代号:HDU 1002 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 原题描述: Problem Description I have a ...
- hdu 1002 A+B
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1002 复习一下大数 模板: #include <stdio.h> #include <s ...
- 杭电acm 1002 大数模板(一)
从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
- hdu 1002.A + B Problem II 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- js修改Switchery复选框的状态
声明一个switchery插件,绑定到一个input上 js代码 var mySwitch = new Switchery($('#blacklist')[0], { size:"small ...
- python -- 安装+pip+requests
python3 安装库 sudo python3 -m pip install beautifulsoup4 步骤1:安装pyenv 为了能顺利的将系统的python和下载的python版本呼唤, ...
- ISO/IEC 9899:2011 条款5——5.1.1 翻译环境
5.1.1 翻译环境 5.1.1.1程序结构 1.一个C程序不需要一次全被翻译完.程序的文本被保存在本国际标准中被称作为源文件(或预处理文件)的单元里.一个源文件连同所有通过指示符#include所包 ...
- web手工项目03-登录功能测试用例及缺陷编写-流程图画法-前后台下单及发货流程图-流程图设计测试用例方法-功能测试涉及到的四种数据库场景
回顾 注册功能测试(步骤,需求分析(输入分析,处理分析,输出分析),数据构造(有效等价类,无效等价类,有效数据,无效数据),编写用例,执行用例,缺陷报告) 轮播图功能测试(步骤,需求分析拆分测试点,测 ...
- Mysql备份工具mysqldump和mysqlhotcopy
(1).Mysql备份类型 1)按照备份时对数据库的影响分为 Hot backup(热备):也叫在线备份.指在数据库运行中直接备份,对正在运行的数据库没有任何影响. Cold backup(冷备):也 ...
- 一秒 解决 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql 问题
提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'.前两天也出现过这个问题,网上找了一个比 ...
- UIFontDownLoad ----动态下载系统提供的字体
程序运行结果如下 : 当点击对应单元格实现下载对应的字体. 控制台打印结果如下 : 2015-10-05 11:14:04.132 UIFontDownLoad[12721:86827] state ...
- windows下配置Sublime Text 2开发Nodejs
1 下载 Sublime Text 2 http://www.sublimetext.com/ 2 下载Nodejs插件,下载ZIP包 https://github.com/tanepiper/Sub ...
- Introduction - Unsupervised Learning
摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第一章<绪论:初识机器学习>中第4课时<无监督学习>的视频原文字幕.为本人在视频学习过程中逐字逐句 ...
- 【数据库开发】Redis数据库服务器启动配置
Redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sor ...