Building Roads
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11861   Accepted: 3376

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 (XiYi) on the plane (0 ≤ X≤ 1,000,000; 0 ≤ Y≤ 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: Xand Y
* 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

一个最小生成树问题,kruskal算法会TLE

没什么可说i的,这玩意得存模板,这题唯一的不一样只是改变了权值为两点间距。

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
struct node
{
double x,y;
}a[1005];
int vis[1005];
double d[1005][1005];
double dis(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double prim()
{
memset(vis,0,sizeof(vis));
double low[1005];
int pos=1;
double ans=0;
vis[1]=1;
for(int i=2;i<=n;i++){
low[i]=d[pos][i]; }
for(int i=1;i<n;i++){
double min=INF;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&min>low[j]){
min=low[j];
pos=j;
}
}
vis[pos]=1;
ans+=min;
for(int i =1;i<=n;i++)
{
if(!vis[i]&&low[i]>d[pos][i]){
low[i]=d[pos][i];
}
}
}
return ans;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(d,INF,sizeof(d));
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
}
for(int i=1;i<=n;i++){
for(int j =i+1;j<=n;j++){
d[i][j]=d[j][i]=dis(a[i],a[j]);
}
}
for(int i=0;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
d[x][y]=0;
d[y][x]=0;
}
printf("%.2f\n",prim());
}
}

  

POJ 3625 最小生成树 Prim C++的更多相关文章

  1. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  2. 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...

  3. 最小生成树Prim算法(邻接矩阵和邻接表)

    最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...

  4. 转载:最小生成树-Prim算法和Kruskal算法

    本文摘自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html 最小生成树-Prim算法和Kruskal算法 Prim算 ...

  5. 最小生成树Prim

    首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注 ...

  6. 最小生成树—prim算法

    最小生成树prim算法实现 所谓生成树,就是n个点之间连成n-1条边的图形.而最小生成树,就是权值(两点间直线的值)之和的最小值. 首先,要用二维数组记录点和权值.如上图所示无向图: int map[ ...

  7. 最小生成树Prim算法和Kruskal算法

    Prim算法(使用visited数组实现) Prim算法求最小生成树的时候和边数无关,和顶点树有关,所以适合求解稠密网的最小生成树. Prim算法的步骤包括: 1. 将一个图分为两部分,一部分归为点集 ...

  8. 最小生成树 Prim Kruskal

    layout: post title: 最小生成树 Prim Kruskal date: 2017-04-29 tag: 数据结构和算法 --- 目录 TOC {:toc} 最小生成树Minimum ...

  9. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

随机推荐

  1. js 小数计算为啥和想象中不一样!

    今天遇到了一个比较有趣的事,如果要你计算0.1+0.2等于多少你会怎么回答? "0.3啊!"你可能都不会考虑.我也一样,当a=0.1,b=0.2时 if(a+b === 0.3){ ...

  2. jaspersoft studio 的初级入门(一)

    前言 最近的工作涉及到企业的报表生成功能,于是就想用此篇博客来记录我的学习历程.进入Jasperreport项目的官网发现有一个软件叫Jaspersoft studio的,它的版本也是6.3.1跟Ja ...

  3. 【Maven】解决linux下安装maven update-alternative --display mvn链接层数过多

    问题描述: 今天首次在linux上安装配置maven,编辑/etc/profile 配置好环境变量之后 使用mvn -v 显示出mvn配置信息,此时以为可以顺利的构建maven项目. 结果中间构建时, ...

  4. Ubuntu 14.04下Redis安装报错:“You need tcl 8.5 or newer in order to run the Redis test”问题解决

    Redis简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工 ...

  5. 用Html模仿百度一下你就知道

    用Html模仿百度一下你就知道.... ------------------------------- <!doctype html> <html lang="en&quo ...

  6. 【Unity游戏开发】浅谈Unity游戏开发中的单元测试

    一.单元测试的定义与作用 单元测试定义:单元测试在传统软件开发中是非常重要的工具,它是指对软件中的最小可测试单元进行检查和验证,一般情况下就是对代码中的一个函数去进行验证,检查它的正确性.一个单元测试 ...

  7. 灾难恢复-boot分区的恢复方法

    boot分区是系统启动中最重要的部分,如果服务器由于病毒攻击又或者被管理员误删除了boot分区.那么就会存在潜在的风险.为什么说是潜在的风险?因为boot分区被删除后系统仍在继续运行,看似无状况但是在 ...

  8. MongoDB环境安装

    ---------------------MongoDB安装环境--------------------- 1.MongoDB下载地址:http://www.mongodb.org/downloads ...

  9. Python Counter class

    Counter class https://docs.python.org/2/library/collections.html#collections.Counter # class collect ...

  10. 项目管理svn

    https://nchc.dl.sourceforge.net/project/tortoisesvn/1.9.6/Application/TortoiseSVN-1.9.6.27867-x64-sv ...