时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:1538

解决:760

题目描述:

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through. 

    Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting
the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

输入:

The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

输出:

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

样例输入:
3
1.0 1.0
2.0 2.0
2.0 4.0
样例输出:
3.41
来源:
2009年北京大学计算机研究生机试真题

思路:

求最小生成树,其中的总路径长度即答案。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h> #define N 100
#define M (N*(N-1)/2) typedef struct point {
double x;
double y;
} POINT; typedef struct node {
int x;
int y;
double d;
} ROAD; int n;
int pre[N+1];
int count[N+1];
int num; void init()
{
for (int i=1; i<=n; i++)
{
pre[i] = i;
count[i] = 1;
}
num = n;
} int find(int i)
{
while (i != pre[i])
i = pre[i];
return i;
} int combine(int i, int j)
{
int a = find(i);
int b = find(j);
if (a != b)
{
if (count[a] > count[b])
{
pre[b] = a;
count[a] += count[b];
count[b] = 0;
}
else
{
pre[a] = b;
count[b] += count[a];
count[a] = 0;
}
num --;
return 1;
}
else
return 0;
} int cmp(const void *a, const void *b)
{
ROAD *x = (ROAD *)a;
ROAD *y = (ROAD *)b;
return (x->d > y->d) ? 1 : -1;
} int main(void)
{
int m, i, j;
POINT p[N+1];
ROAD r[M];
double sum; while (scanf("%d", &n) != EOF && n)
{
for (i=1; i<=n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y); m = 0;
for (i=1; i<=n; i++)
{
for (j=i+1; j<=n; j++)
{
r[m].x = i;
r[m].y = j;
r[m].d = sqrt( (p[i].x-p[j].x)*(p[i].x-p[j].x)
+ (p[i].y-p[j].y)*(p[i].y-p[j].y) );
m ++;
}
}
qsort(r, m, sizeof(r[0]), cmp);
//for (i=0; i<m; i++)
// printf("%d %d %.2lf\n", r[i].x, r[i].y, r[i].d); init();
sum = 0;
for(i=0; i<m; i++)
{
if(combine(r[i].x, r[i].y))
sum += r[i].d;
if (num == 1)
break;
} printf("%.2lf\n", sum);
} return 0;
}
/**************************************************************
Problem: 1144
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:932 kb
****************************************************************/

九度OJ 1144:Freckles(斑点) (最小生成树)的更多相关文章

  1. 【九度OJ】题目1144:Freckles 解题报告

    [九度OJ]题目1144:Freckles 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1144 题目描述: In an ...

  2. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  3. 【九度OJ】题目1028:继续畅通工程 解题报告

    [九度OJ]题目1028:继续畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1028 题目描述: 省政府" ...

  4. 【九度OJ】题目1024:畅通工程 解题报告

    [九度OJ]题目1024:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅 ...

  5. 【九度OJ】题目1017:还是畅通工程 解题报告

    [九度OJ]题目1017:还是畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1017 题目描述: 某省调查乡村交通 ...

  6. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  7. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  8. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  9. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

随机推荐

  1. usaco-Money Systems

    题意: 给出几种硬币,求可用这几种硬币组合出价值为n的方案数.分析: 设dp[i]表示组合出价值i的方案数,则,dp[i]=∑dp[i-val[j]]. #include <iostream&g ...

  2. SQLite复杂表的更新方式

    SQLite复杂表的更新方式   在SQLite中,如果早期设计的表无法满足需要,就需要对表进行更新,如修改名字.添加列.如果针对简单表,修改起来相对容易,直接使用提供的ALTER命令即可.但是如果该 ...

  3. Maximum Product Subarray - LeetCode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. 【BZOJ1562】【jzyzOJ1730】【COGS409】NOI2009变换序列 二分图匹配

    [问题描述]        对于N个整数0, 1, ……, N-1,一个变换序列T可以将i变成Ti,其中 定义x和y之间的距离.给定每个i和Ti之间的距离D(i,Ti), 你需要求出一个满足要求的变换 ...

  5. 瞬发大量并发连接 造成MySQL连接不响应的分析

    http://www.actionsky.com/docs/archives/252  2016年12月7日  黄炎 目录 1 现象 2 猜想 3 检查环境 4 猜想2 5 分析 5.1 TCP握手的 ...

  6. js对象浅拷贝和深拷贝详解

    js对象浅拷贝和深拷贝详解 作者:i10630226 字体:[增加 减小] 类型:转载 时间:2016-09-05我要评论 这篇文章主要为大家详细介绍了JavaScript对象的浅拷贝和深拷贝代码,具 ...

  7. Tiny4412 支持 adb reboot-bootloader

    硬件版本:     Tiny4412ADK + S700 4GB u-boot 版本: u-boot-2010-12 linux版本:        Linux-3.0.8 版本一 支持 adb re ...

  8. f5压缩

    F5应用加速 编辑 F5在4个方面对Web应用提速: 一降低网络传输的压力,最典型技术是压缩 F5的加速技术把100K的页面压缩到20K在广域网上传输,一些标准的浏览器如IE.火狐可以自动解压,这一过 ...

  9. ThinkPHP示例:CURD

    完整的控制器文件: class IndexAction extends Action { // 查询数据 public function index() { $Form = M("Form& ...

  10. ylb:SQL 表的高级查询-多表连接和子查询

    ylbtech-SQL Server: SQL Server-表的高级查询-多表连接和子查询 SQL Server 表的高级查询-多表连接和子查询. 1,ylb:表的高级查询-多表连接和子查询 返回顶 ...