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;
}
buaa 1033 Easy Problem(三分)(简单)的更多相关文章
- D. Easy Problem(简单DP)
题目链接:http://codeforces.com/contest/1096/problem/D 题目大意:给你一个字符串,然后再给你去掉每个字符串的每个字符的花费,然后问你使得字符中不再存在har ...
- POJ 2826 An Easy Problem!(简单数论)
Description Have you heard the fact "The base of every normal number system is 10" ? Of co ...
- 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)
An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- 杭电oj An easy problem
</pre><h1 style="color: rgb(26, 92, 200);">An easy problem</h1><stron ...
- CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?
CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...
- An easy problem
An easy problem Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...
随机推荐
- 记Spring下autowire为name时的一个现象
当autowire为byname时,假如有一个名为“user”的component,恰巧还有另一个属性为“user”,它的set方法为setUser(User user),Spring会强行把comp ...
- [ SDOI 2011 ] 打地鼠
\(\\\) \(Description\) 给出一个\(N\times M\)的矩阵,你可以自由确定一个\(R\times C(R,C>0)\)的矩形,使得可以多个用矩形覆盖整个矩阵,覆盖的定 ...
- P1418 选点问题
题目描述 给出n个点,m条边,每个点能控制与其相连的所有的边,要求选出一些点,使得这些点能控制所有的边,并且点数最少.同时,任意一条边不能被两个点控制 输入输出格式 输入格式: 第一行给出两个正整数n ...
- nginx下如何配置 ssl证书?腾讯云ssl证书为例!
nginx下如何配置 ssl证书?腾讯云ssl证书为例! 目前为止,https已经成为一种趋势,想要开启https就需要ssl证书. 首先,为域名注册ssl证书. 腾讯云注册地址:https://cl ...
- 解决:efi usb device has been blocked by the current security policy
解决:efi usb device has been blocked by the current security policy 问题描述:U盘装系统或者其他操作时,是因为BIOS安全策略,出现上述 ...
- MySQL 多表批量更新
使用inner join 进行表更新sql 与mysql 的区别: SQL UPDATE W SET W.字段=新数据 FROM 表a W INNER JOIN 表B d ON W.wID=D.wid ...
- CentOS7将firewall切换为iptables防火墙
- (5.2.1)配置服务器参数——即时文件初始化(IFI)
关键词:零填充,即时文件初始化 转自:https://www.cnblogs.com/gaizai/p/3516905.html 概念: 所有新申请的空间,sql server都要以0来填充完磁盘文件 ...
- C lstat major MAJOR 获得设备号
#cat lstat.c #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #inc ...
- sysbench测试阿里云CPU
参考 https://wiki.mikejung.biz/Benchmarking 买了一个1核的ECS,测试一下CPU性能 第一次是只用1个thread去跑 [root@iZwz9fy718twfi ...