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 ...
随机推荐
- Lua相关回调总结【转】
原文 http://www.zaojiahua.com/lua-callback-functions.html 最近做一个小项目,是用Lua写的,中间用到了很多的回调,基本Cocos中的那几种常用回调 ...
- scala的Map
package com.test.scala.test object MapTest { def main(args: Array[String]): Unit = { //定义一个不可变的map v ...
- 使用GCD验证码倒计时
__block int timeout = 60; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY ...
- Java—将文件夹压缩为zip文件
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...
- 图解TCP/IP笔记(3)——IP协议
目录 IP协议 IP寻址 IP地址组成 IP地址分类 广播地址 子网掩码 全局地址和私有地址 IP协议 跨越不同数据链路,实现两端节点之间的数据包传输 数据链路:只负责某一个区间之间的通信传输 IP协 ...
- html5——文本阴影
基本结构 text-shadow: 30px 23px 31px #;/* 文字阴影: 水平位移 垂直位移 模糊程度 阴影颜色*/ 凹凸文字 <!DOCTYPE html> <htm ...
- SQL查询性能优化
使用高效的查询 使用 EXISTS 代替 IN -- 查询A表中同时存在B表的数据 -- 慢 SELECT * FROM Class_A WHERE id IN (SELECT id FROM Cla ...
- ARX中类型强制转换
比如 克隆 clone, 获得的是一个acrxobject, acrxobject *pobj=pployline->clone(); acdbpolyline *ppoly=acdbpolyl ...
- Sping装配之——自动装配
Sping从两个角度来实现自动化装配: 组件扫描(component scaning):spring会自动发现应用上下文中所创建的bean; 自动装配(autowiring):spring自动满足be ...
- 安装hiredis后swoole扩展消失
php -m报错: PHP Warning: PHP Startup: Unable to load dynamic library 'swoole' (tried: /home/work/study ...