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. ...
随机推荐
- Java中解决多线程数据安全问题
同步代码块 基本语句 synchronized (任意对象) { 操作共享代码 } 代码示例 public class SellTicket implements Runnable { private ...
- Markdown常用的格式
一级标题 Markdown # Markdown 二级标题 Markdown ## Markdown 三级标题 Markdown ### Markdown 四级标题 Markdown #### Mar ...
- IP实验笔记
代码: 对LSW1: Vlan 10 Interface ethernet 0/0/1 Port link-type access Port default vlan 10 Interface eth ...
- Abp vNext 基础篇丨领域构建
介绍 我们将通过例⼦介绍和解释⼀些显式规则.在实现领域驱动设计时,应该遵循这些规则并将其应⽤到解决⽅案中. 领域划分 首先我们先对比下Blog.Core和本次重构设计上的偏差,可以看到多了一个博客管理 ...
- (数据科学学习手札127)在Python中使用icecream实现高效debug
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 尽管有那么多花里胡哨的debug工具和方式 ...
- 深入浅出Mybatis系列(三)---配置简介(mybatis源码篇)
上篇文章<深入浅出Mybatis系列(二)---Mybatis入门>写了一个Demo简单体现了一下Mybatis的流程.本次,将简单介绍一下Mybatis的配置文件: 上次例子中,我们以 ...
- 莫逸风CSDN文章目录
『Ⅱ』-----随笔 莫逸风CSDN文章目录 The Programmer's Oath程序员的誓言-- 今天突发奇想写了一个小工具,CSDN文章目录生成器 vue去掉一些烦人的校验规则 输入npm ...
- File--字节流--字符流
File类 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 1.构造方法 public File(String pathname) :通过将给定 ...
- Alibaba cloud 3 安装docker
最近因为公司买阿里服务器装的 Alibaba cloud Linux 系统,在部署环境的时候也是遇到各种坑,网上教程大多都是其他系统的,今天就来分享一下自己安装Docker的步骤,同时也是给自己记录一 ...
- 一. Go微服务--隔离设计
1. 前言 隔离设计源于船舶行业,一般而言无论大船还是小船,都会有一些隔板,将船分为不同的空间,这样如果有船舱漏水一般只会影响这一小块空间,不至于把整个船都给搞沉了. 同样我们的软件服务也是一个道理, ...