zoukankan      html  css  js  c++  java
  • hdu 3548 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<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<algorithm>
     5 
     6 using namespace std;
     7 
     8 struct Length
     9 {
    10     int x,y;
    11 }L[1005];
    12 
    13 bool com(Length a,Length b)
    14 {
    15     return a.x<b.x;
    16 }
    17 
    18 int main( ) {
    19 
    20     int N,n,flag,xx=0;
    21     double Min,s1,s2,s3;
    22     cin >>N;
    23     while(N--){
    24         cin >>n;
    25         Min=99999; flag=0;
    26         for( int i=1;i<=n;i++)
    27            scanf("%d %d",&L[i].x,&L[i].y);
    28         sort(L+1,L+n+1,com);
    29         for( int i=1;i<=n-2;i++){
    30             for( int j=i+1;j<=n-1;j++){
    31                 if( Min<=2*(L[j].x-L[i].x) ) break;
    32                 s1=sqrt( ( (L[i].x-L[j].x)*(L[i].x-L[j].x)+(L[i].y-L[j].y)*(L[i].y-L[j].y) )*1.0);
    33                 if( Min<=2*s1 ) continue;
    34                 for( int k=j+1;k<=n;k++){
    35                     int t1=(L[i].x-L[j].x)*(L[j].y-L[k].y), t2=(L[j].x-L[k].x)*(L[i].y-L[j].y);
    36                     if( t1==t2 ) continue;
    37                     flag=1;
    38                     s1=sqrt( ( (L[i].x-L[j].x)*(L[i].x-L[j].x)+(L[i].y-L[j].y)*(L[i].y-L[j].y) )*1.0);
    39                     s2=sqrt( ( (L[k].x-L[j].x)*(L[k].x-L[j].x)+(L[k].y-L[j].y)*(L[k].y-L[j].y) )*1.0);
    40                     s3=sqrt( ( (L[k].x-L[i].x)*(L[k].x-L[i].x)+(L[k].y-L[i].y)*(L[k].y-L[i].y) )*1.0);
    41                     Min= min( Min, s1+s2+s3 );
    42                 }
    43             }
    44         }
    45         cout <<"Case " <<++xx <<": ";
    46         if( flag ) printf("%.3lf
    ",Min);
    47         else printf("No Solution
    ");
    48     }
    49 }
    View Code
     
     
     
  • 相关阅读:
    python-文件操作
    python之-字符编码
    课程总结
    IO流文件输出流的应用
    字符串的基本操作
    数据结构字符串实训报告
    窗口的切换
    事件处理
    Java异常处理
    二维数组实现转置
  • 原文地址:https://www.cnblogs.com/lysr--tlp/p/eee.html
Copyright © 2011-2022 走看看