HDU 1392 Surround the Trees (Graham求凸包周长)

题目链接

题意 : 让你找出最小的凸包周长 。

思路 : 用Graham求出凸包,然后对每条边求长即可。

Graham详解

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <math.h>
 5 #include <algorithm>
 6
 7 using namespace std ;
 8
 9 struct point
10 {
11     int x,y ;
12 }p[110],p1[110];
13 int n ;
14
15 double dis(point a,point b)
16 {
17     return sqrt(double((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))) ;
18 }
19 double cross(point a,point b,point c)
20 {
21     return (a.x-c.x)*(b.y-c.y) - (a.y-c.y)*(b.x-c.x) ;
22 }
23 bool cmp(point a,point b)
24 {
25     if(cross(a,b,p[0]) > 0 || cross(a,b,p[0]) == 0 && dis(a,p[0]) < dis(b,p[0]))
26         return true ;
27     return false ;
28 }
29 void Graham()
30 {
31     int cnt;
32     sort(p+1,p+n,cmp) ;
33     p[n] = p[0] ;
34     p1[0] = p[0] ;
35     p1[1] = p[1] ;
36     cnt = 1 ;
37     for(int i = 2 ; i <= n ; i++)
38     {
39         while(cnt >= 1 && cross(p1[cnt-1],p1[cnt],p[i]) <= 0) cnt -- ;
40         p1[ ++cnt] = p[i] ;
41     }
42     double sum = 0.0 ;
43     for(int i = 0 ; i < cnt ; i++)
44         {
45             sum += dis(p1[i],p1[i+1]) ;
46             //printf("*%.2lf*\n",sum) ;
47         }
48         printf("%.2lf\n",sum) ;
49 }
50 int main()
51 {
52     while(~scanf("%d",&n) && n)
53     {
54         int pos = 0 ;
55         for(int i = 0 ; i < n ; i++)
56         {
57             scanf("%d %d",&p[i].x,&p[i].y) ;
58             if(p[pos].y > p[i].y || (p[pos].y == p[i].y && p[i].x < p[pos].x))
59                 pos = i ;
60         }
61         if(n == 1) {puts("0.00");continue ;}
62         if(n == 2)
63         {
64             printf("%.2lf\n",dis(p[1],p[0])) ;
65             continue ;
66         }
67         swap(p[pos],p[0]) ;
68         //printf("%d %d\n",p[0].x,p[0].y) ;
69         Graham() ;
70     }
71     return 0 ;
72 }

时间: 2024-08-04 15:50:03

HDU 1392 Surround the Trees (Graham求凸包周长)的相关文章

HDU 1392 Surround the Trees(几何 凸包模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1392 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模板.凸包有很多的算法.这里用Adrew. 注意这几组测试数据 1 1 1 3 0 0 1 0 2 0 输出数据 0.00 2.00 1 #include<cmath> 2 #include<cstdio> 3 #include<algorithm> 4 using name

hdu 1392 Surround the Trees (凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7043    Accepted Submission(s): 2688 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

hdu 1392 Surround the Trees(凸包果题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7473    Accepted Submission(s): 2860 Problem Description There are a lot o

HDU 1392 Surround the Trees

PS: 在求解两个点的时候就是两个点的距离,在这WA了一次. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int maxn = 110; struct point { int x, y; point(d

HDU 1392 Surround the Trees (凸包周长)

题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you

POJ 1113 Wall(Graham求凸包周长)

题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <cmath> 5 #include <algorithm> 6 7 const double PI = acos(-1.0) ;

HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9790    Accepted Submission(s): 3763 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDU - 1392 Surround the Trees (凸包)

Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出凸包上的点后,s数组中就是按顺序的点,累加一下距离就是周长了. #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdli

ZOJ - 1453 —— Surround the Trees (求凸包长度)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20416 这道题几乎是纯的“凸包问题”,也让我对于凸包有了入门级的认识. 我看到的比较好的算法是Graham扫描法,用笔模拟了一下该算法,大致就对该算法有所体会了,所以碰到一个新算法,一定要不怕麻烦,通过模拟该算法深入了解其中的奥秘 Graham扫描法                                       Graham扫描法的具体流程: 1. 首先