HDU3548 Enumerate the Triangles(优化)

题 目 :

Problem Description
Little E is doing geometry works. After drawing a lot of points on a plane, he want to enumerate all the triangles which the vertexes are three of the points to find out the one with minimum perimeter. Your task is to implement his work.
 
Input
The input contains several test cases. The first line of input contains only one integer denoting the number of test cases.
The first line of each test cases contains a single integer N, denoting the number of points. (3 <= N <= 1000)
Next N lines, each line contains two integer X and Y, denoting the coordinates of a point. (0 <= X, Y <= 1000)
 
Output
For each test cases, output the minimum perimeter, if no triangles exist, output "No Solution".
 
Sample Input
2
3
0 0
1 1
2 2
4
0 0
0 2
2 1
1 1
 
Sample Output
 
Case 1: No Solution
Case 2: 4.650

题意:

平面上有n(n<=1000)点,问组成的三角形中,周长最小是多少。

优化:

周长c=L1+L2+L3,所以推得①c > 2Li,假设Li的端点为点a、b,则又有Li>=| Xa-Xb |,故②c > 2*| Xa-Xb |。

只是用优化②即可

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 #define INF 1001*1002
 8 struct point{
 9     int x, y;
10 }p[1005];
11 bool cmp(point a, point b){
12     return a.x == b.x ? a.y < b.y : a.x < b.x;
13 }
14 double dis(int i, int j){
15     return sqrt( 1.0*(p[i].x-p[j].x)*(p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y) );
16 }
17 int main()
18 {
19     int i, j, t, r, n;
20     double IJ, IR, JR, C, minC;
21     int cas = 1;
22     cin>>t;
23     while(t--)
24     {
25         cin>>n;
26         for(i = 1; i <= n; i++)
27             scanf("%d%d", &p[i].x, &p[i].y);
28         C = minC = INF;
29         sort(p+1, p+n+1, cmp);
30         for(i = 1; i <= n; i++){
31             for(j = i+1; j <= n; j++){
32                 IJ  = dis(i, j);
33                 /*周长c=L1+L2+L3,所以推得c > 2Li,假设Li的端点为点a、b,
34                   则又有Li>=| Xa-Xb |,故c > 2*| Xa-Xb |*/
35                 if(minC <= 2*(p[j].x-p[i].x))break;//优化②
36                 if(minC <= 2*IJ)continue;//优化①
37 
38                 for(r = j+1; r <= n; r++){
39                     if(minC <= 2*(p[r].x-p[j].x))break;//优化②
40                     IR = dis(i, r);
41                     JR = dis(j, r);
42                     if(IJ + IR > JR && (fabs(IJ-IR) < JR))
43                         C = IJ + IR + JR;
44                     minC = min(C, minC);
45                 }
46             }
47         }
48         printf("Case %d: ", cas++);
49         if(minC == INF)printf("No Solution\n");
50         else printf("%.3lf\n", minC);
51     }
52     return 0;
53 }
View Code

 

转载于:https://www.cnblogs.com/qiu520/p/3663796.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值