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. electron + vue 实践项目

    github地址 本地安装环境准备 安装node: * https://nodejs.org/en/download/ 配置webpack: npm install -g webpack(sudo权限 ...

  2. SpringMVC入门--编写一个SpringMVC小程序

    一.SpringMVC的优势 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一.Spring3.0 后全面超越 Struts2,成为最优秀的 M ...

  3. arm指令bne.w改成b,即无条件跳转

    近期逆向一个程序,需要把bne.w改成b,无条件跳转.由于ios逆向不像pc上,可以在od里直接改汇编指令,这篇文章给了我很大的帮助.通过memory write 修改后,验证可行后,再用ultrae ...

  4. 如何改变Myeclipse编辑区背景色(转)

    编辑窗口右键单击-->Preferences-->General加号-->Editors加号-->点Text Editors字样-->右下窗口选Backgroud col ...

  5. python之路第二天 随便记记 今天主要很郁闷

    为何要有操作系统 为了让程序员更轻松的完成命令电脑工作而存在的,控制硬件,服务于软件. 操作系统的位置 操作系统位于软件和硬件之间.操作系统由内核(运行于内核态,控制硬件)和系统调用(运行于用户态,为 ...

  6. 关于php中的include html文件的问题,为什么html可以在php中执行

    之前在w3shXXl看的教程,上面对include的解释是把指定的文件复制到这条指令执行的地方. 这真是坑到我了..... 在了解mvc的时候,控制器显示视图时需要用include包含html视图文件 ...

  7. [知了堂学习笔记]_JSON数据操作第2讲(JSON的封装与解析)

    上一讲为大家讲了什么是JSON,那么这一讲为大家带来了在WEB项目中JSON的用法,也就是JSON的封装与解析. 此图是数据库中的部分内容 一.JSON封装 所谓的JSON封装,指的是在Servlet ...

  8. 通过VM虚拟机安装Ubuntu server部署flask项目

    1. VM安装Ubuntu server 14.04,系统安装完成后,首先安装pip工具方便之后的包安装,此处需先使用 apt-get install update,apt-get install u ...

  9. Linux上Oracle自动启停方案

    环境 CentOS 6 x86_64, Oracle 11g R2   方案 Oracle在$ORACLE_HOME/bin目录下提供了dbstart和dbshut两个脚本来启动和停止Oracle.d ...

  10. [PSIDE]PeopleSoft PSIDE无法启动因为缺失MSVCR100.dll解决办法

    “The program can’t start because MSVCR100.dll is missing from your computer” 当开发工具是绿色版的时候,在打开PSIDE时很 ...