九度OJ 1144:Freckles(斑点) (最小生成树)
时间限制: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
思路:
求最小生成树,其中的总路径长度即答案。
代码:
#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
****************************************************************/
最新文章
- Alignment trap 解决方法 【转 结合上一篇
- HDU 4758 Walk Through Squares(AC自动机+DP)
- (转)Sqlite 管理工具 SQLiteDeveloper及破解
- JAVA设计模式之依赖倒转原则
- Machine Learning for hackers读书笔记(一)使用R语言
- apache开源项目--JMeter
- MSSQLSERVER数据库- 获取月份的第一天和最后一天
- DexIndexOverflowException: Cannot merge new index 66080 into a non-jumbo instruction!
- EF中读取随机数据的问题
- Java中Calendar的用法
- JF厂V8版本爱彼AP15703,黄家橡树离岸型,超越N厂神器
- JavaScript中的十个难点,你有必要知道。
- XSS闯关游戏准备阶段及XSS构造方法
- Python的循环导入问题
- IE10 解决input file 同一文件不触发onchange事件
- CVE-2018-18820 icecast 栈缓冲区越界写漏洞分析
- bzoj3612 平衡 (dp)
- Apache服务器如何通过.htaccess文件设置防盗链?
- 【黑金ZYNQ7000系列原创视频教程】05.FPGA和ARM的初次结合&mdash;&mdash;LED实验
- Android中的WebView进行直接加载网页(要注意解决权限问题)