zoukankan      html  css  js  c++  java
  • 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
    ");
    50         else printf("%.3lf
    ", minC);
    51     }
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    object detection物体检测基本概念
    anaconda python环境搭建
    Eclipse环境下添加package到工程的classpath
    34-使用函数实现-文件拷贝
    33-使用函数实现-斐波那契数列
    32-简单的位置参数
    31-分段进行文件拷贝
    30-Python文件拷贝
    29-简单的文件对象基础操作
    28-石头剪刀布:三局两胜
  • 原文地址:https://www.cnblogs.com/qiu520/p/3663796.html
Copyright © 2011-2022 走看看