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
  • 相关阅读:
    FreeRTOS 任务栈大小确定及其溢出检测
    FreeRTOS任务优先级说明
    leetcode 263 Ugly Number
    L2,breakfast or lunch
    Redis(2)用jedis实现在java中使用redis
    L1,a private conversation
    Redis(1)在windows环境下的安装和测试
    springMVC的拦截器工作流程
    求交集,差集,并集,善用java的set
    java下发电子邮件demo
  • 原文地址:https://www.cnblogs.com/qiu520/p/3663796.html
Copyright © 2011-2022 走看看