POJ3625Building Roads
Building Roads
Description
Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.
Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Two space-separated integers: Xi and Yi
* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.
Output
* Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.
Sample Input
4 1
1 1
3 1
2 3
4 3
1 4
Sample Output
4.00
思路:简单的prim算法,这里与Prim算法不同的是,考虑到已经有默认边加入集合。

#include <iostream>
#include<cstring>
#include<algorithm>
#include<stdio.h>
#include<cmath>
using namespace std;
double lowcost[1001],dist[1001][1001];
const double inf = 0x3f3f3f3f;
int vis[1010];//vis判断点是否加入到集合中
int n;
double prim(){
//先把第一个点加入
vis[1] = 1;
int index;
//初始化:第一个点到其他点的距离
for(int i = 1; i <= n; ++i)
lowcost[i] = dist[1][i];
lowcost[1] = 0; double sum = 0;
for(int i = 2; i <= n; ++i){//第一个for循环,目的是将所有点加入集合中,由于第一个点已经加入,所以从2开始
double miner = inf;
for(int j = 1; j <= n ; ++j){//第二个for循环,目的是找到最小的一条边
if(!vis[j] && lowcost[j] < miner){
miner = lowcost[j];//使用miner记录最小的距离
index = j;//使用index记录最小边标号
}
}
vis[index] = 1;
sum += miner;// sum += lowcost[index];
for(int j = 1; j <= n ; ++j){
//更新最短距
if(!vis[j] && lowcost[j] > dist[index][j]){
lowcost[j] =dist[index][j];
}
}
}
return sum;
}
int main(){
int m,A,B;
double x[1001],y[1001];
while(cin >> n >> m){
for(int i = 1; i <= n; ++i){
cin >> x[i] >> y[i];
}
memset(dist,inf,sizeof(dist));
memset(lowcost,inf,sizeof(lowcost));
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; ++i){
for(int j = i + 1; j <= n ; ++j){
dist[i][j] = dist[j][i] = sqrt((x[j] - x[i])*(x[j] - x[i]) + (y[j] - y[i])* (y[j] - y[i]));
}
}
for(int i = 1; i <= m ; ++i){
cin >> A >> B;
dist[A][B] = dist[B][A] = 0;
}
for(int i = 1; i <= n; ++i)
dist[i][i] = 0;
printf("%.2f\n",prim());
}
}
POJ3625Building Roads的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列
D. Fools and Foolproof Roads You must have heard all about the Foolland on your Geography lessons. ...
随机推荐
- MapReduce框架原理--Shuffle机制
Shuffle机制 Mapreduce确保每个reducer的输入都是按键排序的.系统执行排序的过程(Map方法之后,Reduce方法之前的数据处理过程)称之为Shuffle. partition分区 ...
- VNC远程重装CentOS7
适用于云服务器,远程安装纯净版的CentOS7.9 脚本执行完成后使用VNC客户端连接 一键重装脚本 #!/bin/bash #Net Reinstall Centos System red='\03 ...
- pthread_cleanup_push与pthread_cleanup_pop的理解
一.为什么会有pthread_cleanup_push与pthread_cleanup_pop: 一般来说,Posix的线程终止有两种情况:正常终止和非正常终止.线程主动调用pthread_exit( ...
- Spring学习02(DI依赖注入)
5.依赖注入(Dependency Injection,DI) 5.1 概念 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 . 注入 : 指Bean对象所依赖的资源 , 由容器 ...
- MyBatis学习06(动态SQL和缓存)
10.动态SQL 10.1 什么是动态SQL 动态SQL指的是根据不同的查询条件 , 生成不同的Sql语句. 官网描述: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或 ...
- 【笔记】numpy.array基础(3)
numpy数组的合并与分割 合并操作 concatenate拼接,只能操作维数一样的数据 且并不局限于两个元素 对于二维数组来说,可以通过控制axis来控制是按照行还是按照列来进行拼接操作,默认为ax ...
- Spring-Boot-Bean的使用,@Repository,@Service,@Controller,@Component
前言 在Spring MVC的时候,我们使用xml来配置bean,如今的Spring boot推荐我们使用元注解的发生,那就听Spring Boot的推荐,下面我就为大家来介绍下Spring Boot ...
- jpa写原生sql-EntityManager
废话不多说 package com.meeno.trainsys.meeting.service; import com.google.common.collect.Lists; import com ...
- Mac 证书错误
在 Mac 操作系统安装 Python 3.6 或以上版本时,可能会遇到证书错误:Error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify ...
- [转]用C++实现插件体系结构
本文讨论一种简单却有效的插件体系结构,它使用C++,动态链接库,基于面向对象编程的思想.首先来看一下使用插件机制能给我们带来哪些方面的好处,从而在适当时候合理的选择使用. 1. 增强代码的透明度与一致 ...