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 ...
随机推荐
- Android Studio 3.5新特性
Android Studio 3.5新特性 原文链接:https://blog.csdn.net/jklwan/article/details/99974869 Android Studio ...
- Python Re 模块超全解读
re模块下的函数 compile(pattern):创建模式对象 import re pat=re.compile('A') m=pat.search('CBA') #等价于 re.search('A ...
- ISO/IEC 9899:2011 条款6.4.8——预处理数字
6.4.8 预处理数字 语法 1.pp-number: digit . digit pp-number digit pp-number identifier-nondigit pp- ...
- 03--STL算法(常用算法)
一:常用的查找算法 (一)adjacent_find():邻接查找 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-en ...
- Linux 系统中部署 LNMP 高可用负载均衡架构集群实现动态博客
(一)设计思路 高可用:keepalived 解决方案 负载均衡:(lvs)DR做轮询,需要一个调度器,后端节点两个(部署nginx动态博客),通过一个vip去访问动态博客 后端节点需要部署动态博客作 ...
- web框架之MVC/MTV
MVC框架 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式 Model(模型)表示应用程序核心(比如数据库记录列表) Vi ...
- C++: Unresolved external symbol __imp__fscanf
VS2019中遇到错误error LNK2001: unresolved external symbol __imp__fscanf 解决办法:链接legacy_stdio_definitions.l ...
- (十二)class文件结构:魔数和版本
一.java体系结构 二.class格式文件概述 class文件是一种8位字节的二进制流文件, 各个数据项按顺序紧密的从前向后排列, 相邻的项之间没有间隙, 这样可以使得class文件非常紧凑, 体积 ...
- jQuery插件—获取URL参数
做的项目中需要用到通过JS获取GET参数,上网找了一下,找到如下插件: 例如 当前你的URL是: http://www.xxx.com/index.php?test=1&kk=2 //如果想获 ...
- 第一章 Shiro简介——《跟我学Shiro》
转发地址:https://www.iteye.com/blog/jinnianshilongnian-2018936 目录贴:跟我学Shiro目录贴 1.1 简介 Apache Shiro是Java ...