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. Javaweb向服务器上传文件以及从服务器下载文件的方法

    先导入jar包 点击下载 commons-fileupload是Apache开发的一款专门用来处理上传的工具,它的作用就是可以从request对象中解析出,用户发送的请求参数和上传文件的流. comm ...

  2. XSS分析及如何预防

    XSS分析及如何预防 简单说明: XSS(Cross Site Scripting),又称跨站脚本,XSS的重点不在于跨站点,而是在于脚本的执行.在WEB前端应用日益发展的今天,XSS漏洞尤其容易被开 ...

  3. zoj3778 Talented Chef

    As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. ...

  4. SSH:分页实现

    StudentAction: public class StudentAction extends ActionSupport { // 初始化下拉列表 @Resource private Stude ...

  5. python 三级联动

       china_map ={  "华南":{   "广东":["广州市","佛山市","深圳市", ...

  6. django模板(过滤器)

    -------------------django内建的过滤器-------------------1.add 使用形式为:{{ value | add: "2"}} 意义:将va ...

  7. SVG图案

    前面的话 给SVG元素应用填充和描边,除了使用纯色和渐变外,还可以使用图案.本文将详细介绍SVG图案 概述 <pattern>可以实现重复的效果,在canvas中被翻译为模式,而在SVG中 ...

  8. vim插件ctags的安装与使用

    LINUX系统下看程序或者编程序时,看到一个函数经常需要知道该函数的定义,这时ctags就派上用场了,其安装和使用方法如下: 安装方法: sudo apt-get install ctags (ubu ...

  9. Day-11: IO编程

    由于CUP的运行速度远高于其他外设,IO操作有两种方式: 同步IO:CUP登着,程序暂停直到执行完后续代码 异步IO:CUP不等待,去做其他的事情,磁盘做完该做的事情后,告诉CUP,CUP再进行后续代 ...

  10. sql处理null值

    IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值. select (case when ...