东大OJ-Prim算法

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;
}

转载于:https://www.cnblogs.com/weiyinfu/p/5013896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值