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. CentOS 修改固定IP地址

    CentOS 修改固定IP地址 参考地址:https://www.cnblogs.com/technology-huangyan/p/9146699.htmlhttps://blog.csdn.net ...

  2. selinux 开启和关闭

    对于新手来说,linux的selinux困扰了一大批学员,开启后,导致文件权限修改不了等问题,下面就是关闭设置setlinux的方法 查看SELinux状态: 1./usr/sbin/sestatus ...

  3. WebService(axis2),整合springmvc

    webservice:不同组织或者部门之间互通数据 https://www.cnblogs.com/buggou/p/8183738.html 1 package com.sh.test; 2 3 4 ...

  4. 表单Content-Type为multipart/form-data时,后台数据的接收

    我们在写form提交表单的时候,后台大多数用request.getParameter的方式来接收前台输入的数据.但如果我们表单中提交的数据包含file文件传输的话,我们需要将Content-Type改 ...

  5. 设备树中指定的中断触发方式与request_irq中指定的触发方式不一致时,内核会使用哪种中断触发方式呢?

    答:会使用request_irq中指定的触发方式

  6. osg HUD 前景色

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...

  7. web手工项目03-登录功能测试用例及缺陷编写-流程图画法-前后台下单及发货流程图-流程图设计测试用例方法-功能测试涉及到的四种数据库场景

    回顾 注册功能测试(步骤,需求分析(输入分析,处理分析,输出分析),数据构造(有效等价类,无效等价类,有效数据,无效数据),编写用例,执行用例,缺陷报告) 轮播图功能测试(步骤,需求分析拆分测试点,测 ...

  8. 测试理论03-svn-缺陷及报告-jira

    回顾--用例设计方法 边界值(三个点,使用步骤) 判定表(适用,四个组成部分,规则,使用) 因果图(适用,四个符号,使用) 正交表(定义,特性,L_n(m^k),因素,水平,使用,工具allpairs ...

  9. 解决微信小程序textarea层级太高遮挡其他组件的问题

    <view class='remark'> <view class='title'> 备注说明 </view> <textarea class='mark_t ...

  10. 【Leetcode_easy】657. Robot Return to Origin

    problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...