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. ...
随机推荐
- SpringMVC学习01(什么是SpringMVC)
1.什么是SpringMVC 1.1 回顾MVC 1.1.1 什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据 ...
- 安鸾CTF Writeup PHP代码审计01
PHP代码审计 01 题目URL:http://www.whalwl.xyz:8017 提示:源代码有泄露 既然提示有源代码泄露,我们就先扫描一遍. 精选CTF专用字典: https://github ...
- DVWA(一):关于DVWA的基本介绍
一.关于DVWA的搭建及报错问题: 传送门 上面链接主要解决安装DVWA报错的问题,这里防止自己再去找,所以记一下. (1)安装DVWA需要一个web环境,我实在win2003系统(xss_uploa ...
- 记一次Orika使用不当导致的内存溢出
hprof 文件分析 2021-08-24,订单中心的一个项目出现了 OOM 异常,使用 MemoryAnalyzer 打开 dump 出来的 hprof 文件,可以看到 91.27% 的内存被一个超 ...
- 【设计模式】装饰者模式(DecoratorMode0
From: https://liudongdong1.github.io/ 装饰者模式(Decorator Pattern):动态地给一个对象增加一些额外的职责,增加对象功能来说,装饰模式比生成子类实 ...
- 分布式redis自增
redis+springboot RedisUtil.java package com.meeno.chemical.common.redis; import java.util.Date; impo ...
- javascript html 鼠标放大镜效果
1.鼠标放大镜效果 鼠标放大镜效果,将鼠标移入到左图片,则可以在其右边看到放大的图片,且鼠标移动滑块的大小即为右图显示图片.实际效果如下图所示: <!DOCTYPE html> < ...
- C#实现http协议GET、POST请求
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Ne ...
- 【java虚拟机】几种内存溢出及解决方案
作者:平凡希 地址:https://www.cnblogs.com/xiaoxi/p/7406903.html 1.JVM Heap(堆)溢出:java.lang.OutOfMemoryError: ...
- Int 2e 与 Sysenter区别
参考:张银奎<软件调试>第八章 Int 2e: Windows将2e号向量专门用作系统调用,在启动早起初始化中断描述表时便注册好了适合的服务例程.因此当NtDll中的NtReadFile发 ...