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. ...
随机推荐
- OpenResty Lua钩子调用完整流程
前面一篇文章介绍了Openresty Lua协程调度机制,主要关心的是核心调度函数run_thread()内部发生的事情,而对于外部的事情我们并没有涉及.本篇作为其姊妹篇,准备补上剩余的部分.本篇将通 ...
- WPF自定义控件三:消息提示框
需求:实现全局消息提示框 一:创建全局Message public class Message { private static readonly Style infoStyle = (Style)A ...
- Python--数据存储
新建测试文档测试.txt内容如下: 张三:今天天气真好. 李四:是的,真的太好了. 张三:阳光明媚. 李四:鸟语花香. 王五:装逼现场:请带好安全帽 张三:难得好天气,今天就不偷懒了. 李四:能把乞讨 ...
- Shell-13-常用文件目录
linux系统目录结构 环境变量文件 系统级 系统级变量文件对所有用户生效 #系统范围内的环境变量和启动文件. #不建议把要做的事情写在这里面,最好创建一个自定义的,放在/etc/profile.d ...
- Java HashSet和TreeSet【笔记】
Java HashSet和TreeSet[笔记] PS:HashSet.TreeSet 两个类是在 Map 的基础上组装起来的类 HashSet 类注释 1.底层实现基于 HashMap,所以迭代时不 ...
- 用三个while循环和tkinter实现一个显示屏
用三个while循环和tkinter实现一个显示屏 import tkinter as tk import time # 输入框是跟程序打交道的一个途径,例如程序要求你输入账号密码,那么它就需要提供两 ...
- java导出excel(easypoi)
介绍 easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 ...
- SQL Server中的distinct(不允许重复)
参考网址:https://blog.csdn.net/tswc_byy/article/details/81835023 一.建库和建表create database scortuse scortcr ...
- ASP net core面试题汇总及答案
在dot net core中,我们不需要关心如何释放这些服务, 因为系统会帮我们释放掉.有三种服务的生命周期. 单实例服务, 通过add singleton方法来添加.在注册时即创建服务, 在随后的请 ...
- linux 常用命令(三)——(centos7)MySql 5.7添加用户、删除用户与授权
一.创建用户:以root用户登录到数据库进行用户创建 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例如: CREATE US ...