1222: Sweep the snow

时间限制: 1 Sec  内存限制: 128 MB

提交: 28  解决: 18

[提交][状态][讨论版]

题目描述

After the big big snow yesterday, NEU became a beautiful silver world.In the morning, poor michaelalan got a short message which told him to sweep the whole school snow and make the traffic unblocked. As you see, it needs so much time to do
by one person.There are some points need clear,and these points must connected together(eg:If there are A point and B point, then sweep a line between A and B is enough). So he wants to know the minimum length of snow he needs to sweep to ensure the traffic
unblocked (the width of snow is ignored).

输入

Multiple inputs, process until the EOF.

First line there is a integer n (2<=n<=1000) which means the count of points needs clear.then following n lines with each line two double numbers indicate the x-coordinate and the y-coordinate of the point.

输出

The minimum length of snow need swept(reserve one decimal, %.1lf).

样例输入

30.0 0.01.0 1.01.0 0.041.0 0.00.0 0.00.0 1.01.0 1.080.0 0.02.5 3.51.1 1.19.0 10.03.1 1.35.5 3.50.0 3.03.0 0.0

样例输出

2.03.019.7
#include<stdio.h>
#include<string.h>
#include<float.h>
#include<math.h>
struct point{ double x, y; };
point a[1000];
double dis[1000];
int n;
double distance(int i, int j){
	double x = a[i].x - a[j].x;
	double y = a[i].y - a[j].y;
	return sqrt(x*x + y*y);
}
double prim(){
	int i, next=n,now=0;
	double cost = 0;
	for (i = 1; i <= n; i++)dis[i] = DBL_MAX;
	dis[0] = 0;
	while (1){
		cost += dis[now];
		dis[now] = -1;
		for (i = 0; i < n; i++){
			if (dis[i] == -1)continue;
			double temp = distance(now, i);
			if (temp < dis[i])dis[i] = temp;
			if (dis[i] < dis[next])next = i;
		}
		if (next == n)return cost;
		now = next;
		next = n;
	}
}
int main(){
	freopen("in.txt", "r", stdin);
	while (scanf("%d", &n)!=-1){
		int i;
		for (i = 0; i < n; i++)
			scanf("%lf%lf", &a[i].x, &a[i].y);
		printf("%.1lf\n",prim());
	}
	return 0;
}

东大OJ-Prim算法的更多相关文章

  1. SWUST OJ 1075 求最小生成树(Prim算法)

    求最小生成树(Prim算法) 我对提示代码做了简要分析,提示代码大致写了以下几个内容 给了几个基础的工具,邻接表记录图的一个的结构体,记录Prim算法中最近的边的结构体,记录目标边的结构体(始末点,值 ...

  2. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  3. 最小生成树のprim算法

    Problem A Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

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

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

  5. 最小生成树——prim算法

    prim算法是选取任意一个顶点作为树的一个节点,然后贪心的选取离这棵树最近的点,直到连上所有的点并且不够成环,它的时间复杂度为o(v^2) #include<iostream>#inclu ...

  6. 洛谷 P3366 【模板】最小生成树 prim算法思路 我自己的实现

    网上有很多prim算法  用邻接矩阵 加什么lowcost数组 我觉得不靠谱 毕竟邻接矩阵本身就不是存图的好方法 所以自己写了一个邻接表(边信息表)版本的  注意我还是用了优先队列  每次新加入一个点 ...

  7. 最小生成树算法——prim算法

    prim算法:从某一点开始,去遍历相邻的边,然后将权值最短的边加入集合,同时将新加入边集中的新点遍历相邻的边更新边值集合(边值集合用来找出新的最小权值边),注意每次更新都需将cost数组中的点对应的权 ...

  8. 贪心算法-最小生成树Kruskal算法和Prim算法

    Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...

  9. Prim算法(三)之 Java详解

    前面分别通过C和C++实现了普里姆,本文介绍普里姆的Java实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里姆算法的代码说明 4. 普里姆算法的源码 转载请注明出处:http:// ...

  10. Prim算法(二)之 C++详解

    本章是普里姆算法的C++实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里姆算法的代码说明 4. 普里姆算法的源码 转载请注明出处:http://www.cnblogs.com/sk ...

随机推荐

  1. docker-7 docker在阿里云的使用

    在传统模式中,开发团队在开发环境中完成软件开发,自己做了一遍单元测试, 测试通过,ᨀ交到代码版本管理库.运维把应用部署到测 试环境, QA 进行测试,没问题后通知部署人员发布到生产环境. 在上述过程中 ...

  2. [Top-Down Approach] Assignment 1: WebServer [Python]

    Today I complete Socket Programming Assignment 1 Web Server Here is the code: #!/usr/bin/python2.7 # ...

  3. C#语言数据总结

    整数类型 sbyte   -128~127之间 byte   0~255 short(Int16) -32768~32768 ushort(UInt16)  0~65535 Int (Int32)   ...

  4. C++ 之 重载赋值操作符

    Widget 类中,定义了一个 Bitmap 类型的私有数据成员 -- pb 指针 class Bitmap { ... }; class Widget { private: Bitmap *pb; ...

  5. Java开发之JSP指令

    一.page指令 page指令是最常用的指令,用来说明JSP页面的属性等.JSP指令的多个属性可以写在一个page指令里,也可以写在多个指令里.但需要注意的是,无论在哪个page指令里的属性,任何pa ...

  6. ATM模拟程序

    一个很简单的ATM模拟程序 #include <stdio.h> void chaxun(int a3){ int b; b=a3; printf("您的余额为:%d\n&quo ...

  7. extra增强延迟加载

    这种配置和配置为lazy=true是一样的,但它的好处在于调用size/contains等方法时,并不查询整个集合的数据,而是发送一条sql语句来处理,只有真正在使用时才全部去查询整个集合

  8. k-d tree 学习笔记

    以下是一些奇怪的链接有兴趣的可以看看: https://blog.sengxian.com/algorithms/k-dimensional-tree http://zgjkt.blog.uoj.ac ...

  9. 转:windows命令行下如何查看磁盘空间大小

    转自:http://www.cnblogs.com/hanxianlong wmic DiskDrive get Size /value ::查看C盘 wmic LogicalDisk where & ...

  10. 条件变量pthread_cond_t怎么用

    #include <pthread.h> #include <stdio.h> #include <stdlib.h> pthread_mutex_t mutex ...