buaa 1033 Easy Problem(三分)(简单)

Easy Problem

时间限制:1000 ms  |  内存限制:65536 KB

描述

In this problem, you‘re to calculate the distance between a point P(xp, yp, zp) and a segment (x1, y1, z1) ? (x2, y2, z2), in a 3D space, i.e. the minimal distance from P to any point Q(xq, yq, zq) on the segment (a segment is part of a line).

输入

The first line contains a single integer T (1 ≤ T ≤ 1000), the number of test cases. Each test case is a single line containing 9 integers xp, yp, zp, x1, y1, z1, x2, y2, z2. These integers are all in [-1000,1000].

输出

For each test case, print the case number and the minimal distance, to two decimal places.

样例输入

3

0 0 0 0 1 0 1 1 0

1 0 0 1 0 1 1 1 0

-1 -1 -1 0 1 0 -1 0 -1

样例输出

Case 1: 1.00

Case 2: 0.71

Case 3: 1.00

题意:

为在一条线段上找到一点,与给定的P点距离最小。很明显的凸性函数,用三分法来解决。dist函数即为求某点到P点的距离。注意精度问题。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#define eps 1e-8
using namespace std;

typedef struct node
{
    double x,y,z;
}node;
node l,r,p;

double dist(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}

int sgn(double a)
{
    return (a>eps)-(a<-eps);
}

node getmid(node a,node b)
{
    node mid;
    mid.x=(a.x+b.x)/2;
    mid.y=(a.y+b.y)/2;
    mid.z=(a.z+b.z)/2;
    return mid;
}

node search()
{
    node mid,midmid;
    while(sgn(dist(l,r))>0)
    {
        mid=getmid(l,r);
        midmid=getmid(mid,r);
        if(dist(p,mid)<dist(p,midmid))
            r=midmid;
        else
            l=mid;
    }
    return r;
}

int main()
{
    int t;node k;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        cin>>p.x>>p.y>>p.z;
        cin>>l.x>>l.y>>l.z;
        cin>>r.x>>r.y>>r.z;
        k=search();
        printf("Case %d: %.2lf\n",i,dist(k,p));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-07 09:46:53

buaa 1033 Easy Problem(三分)(简单)的相关文章

POJ 2826 An Easy Problem!(简单数论)

Description Have you heard the fact "The base of every normal number system is 10" ? Of course, I am not talking about number systems like Stern Brockot Number System. This problem has nothing to do with this fact but may have some similarity. Y

HDU2132 An easy problem 【Java】

An easy problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9194    Accepted Submission(s): 2491 Problem Description We once did a lot of recursional problem . I think some of them is easy

HDU2601,An easy problem

C - An easy problem Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description When Teddy was a child , he was always thinking about some simple math problems ,such as "What it's 1 cup of water plus 1 p

an easy problem(贪心)

An Easy Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8333   Accepted: 4986 Description As we known, data stored in the computers is in binary form. The problem we discuss now is about the positive integers and its binary form.

HDU2123 An easy problem【水题】

An easy problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5922    Accepted Submission(s): 4122 Problem Description In this problem you need to make a multiply table of N * N ,just like th

HDU2132 An easy problem【水题】

An easy problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10149    Accepted Submission(s): 2689 Problem Description We once did a lot of recursional problem . I think some of them is easy

poj2826An Easy Problem?!

链接 繁琐细节题. 1.线段无交点时,ans=0; 2.如图 假设过p3.y的水平线与p1p2相交 因为雨是垂直下落的,左图的情况是无法收集到雨水的 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath&g

Poj 2826 An Easy Problem?!

地址:http://poj.org/problem?id=2826 题目: An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 2003 Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails tw

FZU 1753-Another Easy Problem(求多个组合数的最大公约数)

Another Easy Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 1753 Appoint description:  xietutu  (2013-03-13)System Crawler  (2015-04-27) Description 小TT最近学习了高斯消元法解方程组,现在他的问题来了,如果是以下的方程,