POJ 2560 Freckles(最小生成树)

原题地址:http://poj.org/problem?id=2560

Freckles


Time Limit: 1000MS

 
Memory Limit: 65536K


Total Submissions: 7863

 
Accepted: 3776

Description

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.

Input

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.

Output

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

Sample Input

3

1.0 1.0

2.0 2.0

2.0 4.0

Sample Output

3.41

题意:给你n个直角坐标系的点,求最小生成树的权重和。

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 const int N = 110, Infinity = 0x3f3f3f3f;
 5 struct Point
 6 {
 7     double x;
 8     double y;
 9 }vertex[N];
10 double distance[N][N], D[N];
11 bool cnt[N] = {0};
12 int main(void)
13 {
14     int n, start, mintag;
15     double sum, x, y;
16     while(~scanf("%d", &n))
17     {
18         for(int i=0; i<n; ++i)
19             scanf("%lf %lf", &vertex[i].x, &vertex[i].y);
20         for(int i=0; i<n; ++i)
21         {
22             distance[i][i] = Infinity;
23             for(int j=i+1; j<n; ++j)
24             {
25                 x = vertex[i].x - vertex[j].x;
26                 y = vertex[i].y - vertex[j].y;
27                 distance[i][j] = sqrt(x*x + y*y);
28                 distance[j][i] = distance[i][j];
29             }
30         }
31         sum = 0;
32         start = 0;
33         cnt[0] = true;
34         memcpy(D, distance[0], sizeof(D));
35         for(int i=1; i<n; ++i)
36         {
37             for(int j=1; j<n; ++j)
38             {
39                 if(!cnt[j] && distance[start][j] < D[j])
40                     D[j] = distance[start][j];
41             }
42             mintag = 0;
43             for(int j=1; j<n; ++j)
44             {
45                 if(cnt[j])
46                     continue;
47                 if(D[j] < D[mintag])
48                     mintag = j;
49             }
50             cnt[mintag] = true;
51             sum += D[mintag];
52             start = mintag;
53         }
54         memset(cnt, 0, sizeof(cnt));
55         printf("%.2lf\n", sum);
56     }
57 }
时间: 2024-10-14 04:35:38

POJ 2560 Freckles(最小生成树)的相关文章

POJ 2560 Freckles Prime算法题解

本题是求最小生成树. 给出的是坐标节点,然后需要根据这些坐标计算出各个点之间的距离. 除此就是标准的Prime算法了,能使用Prime的基本上都可以使用Kruskal. 这些经典的算法一定要多写,熟练掌握,否则很难灵活运用的. 而且经典的算法之所以为经典,原因之一是没那么容易自己凭空想象出来的,所以要熟练. #include <stdio.h> #include <string.h> #include <queue> #include <float.h> #

poj 2560 Freckles

题目连接 http://poj.org/problem?id=2560 Freckles Description 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 Ri

POJ 2560 Freckles Prime问题解决算法

这个问题正在寻求最小生成树. 给定节点的坐标,那么我们需要根据各个点之间的这些坐标来计算距离. 除了这是标准的Prime算法的,能源利用Prime基本上,你可以使用Kruskal. 经典的算法必须填写,熟练度.否则它是非常困难的利用. 并且经典的算法之所以为经典.原因之中的一个是没那么easy自己凭空想象出来的,所以要熟练. #include <stdio.h> #include <string.h> #include <queue> #include <floa

poj 1679 判断最小生成树是否唯一

/* 只需判断等效边和必选边的个数和n-1的关系即可 */ #include<stdio.h> #include<stdlib.h> #define N 110 struct node { int u,v,w; }f[N*N*2]; int cmp(const void *a,const void*b) { return (*(struct node *)a).w-(*(struct node *)b).w; } int pre[N]; int find(int x) { if(x

poj 3723 Conscription(最小生成树拓展)

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7702   Accepted: 2667 Description Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be

POJ 2485-Highways(最小生成树prim)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22433   Accepted: 10341 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

poj 1861(最小生成树)

Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access

poj 2485(prim最小生成树)

Highways Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways

POJ 1258 Agri-Net (最小生成树+Prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39820   Accepted: 16192 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee