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. luogu1001 A+B Problem

    A+B Problem 题目描述 输入两个整数a,b,输出它们的和(|a|,|b|<=10^9). 注意 1.pascal使用integer会爆掉哦! 2.有负数哦! 3.c/c++的main函 ...

  2. css知识点

    css知识点 一.盒模型知识 border: 边框 border-width:边框的宽度 border-color:边框的颜色 border-style:边框的线型 border-top:上边框 bo ...

  3. 无法远程连接mysql,连接后也没有权限创建数据库

    问题现象:无法远程连接mysql,连接后也没有权限创建数据库 问题原因: MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1)来连接访问. 这对于 Web- ...

  4. ubuntu安装mysql可视化工具MySQL-workbench及简单操作

    一.使用命令行在ubuntu下安装mysql可视化工具MySQL-workbench Step1:安装MySQL-workbench 方案一:如果你已经装好mysql的相关服务,那么直接使用如下命令即 ...

  5. php sql uuid 32位

    最近表中id需要用到此值来作为唯一主键 其含义是通用唯一识别码.具体好处及应用可百度百科,链接给你https://baike.baidu.com/item/UUID/5921266?fr=aladdi ...

  6. Azure Storage Rest API Demo

    本文主要介绍如何使用C#基于Rest API 操作中国版Microsoft Azure Storage,涉及方法Put Blob.Get Blob以及Delete Blob,其它方法参考上面三种方法适 ...

  7. PhoneGap + Dreamweaver 5.5 无法在模拟器中打开的问题

    版权声明:本文为博主原创文章,未经博主允许不得转载. 原博客地址为:http://blog.csdn.net/dupang/article/details/8248335 按照网上的教程搭建Dream ...

  8. app端性能测试笔记

     IOS不清楚,我就说说android平台吧 1.按不同维度  APP级性能.代码级性能      app这一级   GT啊  emmage都可以检测 2.代码级性能的话  有可以分几块 函数性能UI ...

  9. PHP(Math的调用)

    <script> //数学函数(用Math来调用)://round=四舍五入最接近的整数// var l = 1.1;// var y1 = Math.round(l);// docume ...

  10. (八)、vpn-pptp部署

    中小型规模网站集群架构:VPN-PPTP : 矮哥linux运维群:93324526 前言: 你想管理机器吗? 你想更安全吗? 请安装VPN吧 部署 1.查看系统是否支持PPP [root@oldbo ...