[POJ][1755][Triathlon][半平面交]

  
    
/*
题目:Triathlon
题目来源:POJ 1755
题目难度:中等偏难
题目内容或思路:
半平面交
此题对精度要求很高
计算几何的题目能特判的就特判这样可以在一定程度上避免精度问题
做题日期:2011.2.25
*/
#include
<cstdio>
#include
<cstdlib>
#include
<climits>
#include
<iostream>
#include
<algorithm>
#include
<cstring>
#include
<string>
#include
<queue>
#include
<map>
#include
<vector>
#include
<bitset>
#include
<cmath>
#include
<set>
using namespace std;

const double eps = 1e-8;
const double maxl = 1e10;
const int N = 110;

struct cpoint {
double x, y;
};

int dcmp(double x) {
if (x < -eps) return -1; else return x > eps;
}

double cross(cpoint p0, cpoint p1, cpoint p2) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

int EqualPoint(cpoint p0, cpoint p1) {
return dcmp(p0.x - p1.x) == 0 && dcmp(p0.y - p1.y) == 0;
}

struct cvector {
cpoint s, e;
double ang, d;
};

void setline(double x1,double y1,double x2,double y2,cvector &v) {
v.s.x
= x1; v.s.y = y1;
v.e.x
= x2; v.e.y = y2;
v.ang
= atan2(y2 - y1, x2 - x1);
if (dcmp(x1 - x2))v.d = (x1 * y2 - x2 * y1) / fabs(x1 - x2);
else v.d = (x1 * y2 - x2 * y1) / fabs(y1 - y2);
}


bool parallel(const cvector &a, const cvector &b) {
double u = (a.e.x - a.s.x) * (b.e.y - b.s.y)
- (a.e.y - a.s.y) * (b.e.x - b.s.x);
return dcmp(u) == 0;
}

cpoint CrossPoint(
const cvector &a, const cvector &b) {
cpoint res;
double u = cross(a.s, a.e, b.s), v = cross(a.e, a.s, b.e);
res.x
= (b.s.x * v + b.e.x * u) / (u + v);
res.y
= (b.s.y * v + b.e.y * u) / (u + v);
return res;
}

bool VecCmp(const cvector &l, const cvector &r) {
if (dcmp(l.ang - r.ang)) return l.ang < r.ang;
return l.d < r.d;
}

cvector deq[N];

void HalfPanelCross(cvector vec[], int n, cpoint cp[], int &m) {
int i, tn; m = 0;
sort(vec, vec
+ n, VecCmp);
for (i = tn = 1; i < n; ++i)
if (dcmp(vec[i].ang - vec[i - 1].ang) != 0)
vec[tn
++] = vec[i];
n
= tn;
int bot = 0, top = 1;
deq[
0] = vec[0];
deq[
1] = vec[1];
for (i = 2; i < n; ++i) {
if (parallel(deq[top], deq[top - 1]) ||
parallel(deq[bot], deq[bot
+ 1]) ) return;
while ( bot < top && dcmp( cross(vec[i].s, vec[i].e,
CrossPoint(deq[top], deq[top
- 1])) ) < 0 )
top
--;
while ( bot < top && dcmp( cross(vec[i].s, vec[i].e,
CrossPoint(deq[bot], deq[bot
+ 1])) ) < 0 )
bot
++;
deq[
++top] = vec[i];
}
while ( bot < top && dcmp( cross(deq[bot].s, deq[bot].e,
CrossPoint(deq[top], deq[top
- 1])) ) < 0 )
top
--;
while ( bot < top && dcmp( cross(deq[top].s, deq[top].e,
CrossPoint(deq[bot], deq[bot
+ 1])) ) < 0 )
bot
++;
if (top <= bot + 1) return;
for (i = bot; i < top; ++i)
cp[m
++] = CrossPoint(deq[i], deq[i + 1]);
if (bot < top + 1)
cp[m
++] = CrossPoint(deq[bot], deq[top]);
m
= unique(cp, cp + m, EqualPoint) - cp;
for (i = 0; i < m; ++i) {
if (dcmp(cp[i].x) == 0) cp[i].x = 0;
if (dcmp(cp[i].y) == 0) cp[i].y = 0;
}
}

double PolygonArea(cpoint p[], int n) {
if (n < 3) return 0;
double s = p[0].y * (p[n - 1].x - p[1].x);
for (int i = 1; i < n; ++i)
s
+= p[i].y * (p[i - 1].x - p[(i + 1) % n].x);
return fabs(s / 2);
}

int n;
int u[N], v[N], w[N];
cvector vec[N];
cpoint cp[N];

bool build(double a, double b, double c, int &n) {
double x1, y1, x2, y2;
if (dcmp(b) == 0) {
if (dcmp(a) == 0) {
return c < 0;
}
x1
= x2 = -c / a;
if (dcmp(a) > 0) {
y1
= 0; y2 = 10;
}
else {
y1
= 10; y2 = 0;
}
setline(x1, y1, x2, y2, vec[n
++]);
}
else {
x1
= 0;
y1
= -c / b;
x2
= 1;
y2
= -(a + c) / b;
if (dcmp(b) > 0) {
swap(x1, x2);
swap(y1, y2);
}
setline(x1, y1, x2, y2, vec[n
++]);
}
return true;
}

void solve() {
int m;
for (int i = 0; i < n; ++i) {
scanf(
"%d%d%d", u + i, v + i, w + i);
}
for (int i = 0; i < n; ++i) {
int t = 0;
setline(
0, 0, maxl, 0, vec[t++]);
setline(maxl,
0, maxl, maxl, vec[t++]);
setline(maxl, maxl,
0, maxl, vec[t++]);
setline(
0, maxl, 0, 0, vec[t++]);
for (int j = 0; j < n; ++j) {
if (i == j) continue;
if (u[j] >= u[i] && v[j] >= v[i] && w[j] >= w[i]) {
// 这个if在一定程度上避免了一些精度问题,没有加这句的时候因为精度问题一直WA
t = 0;
break;
}
if (!build(1.0 / u[i] - 1.0 / u[j], 1.0 / v[i] - 1.0 / v[j],
1.0 / w[i] - 1.0 / w[j], t)) {
t
= 0;
break;
}
}
HalfPanelCross(vec, t, cp, m);
double s = PolygonArea(cp, m);
if (s < 1e-16) puts("No"); //这里面积要小于1e-16是一个trick
else puts("Yes");
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen(
"D:\\in.txt", "r", stdin);
#endif
while (scanf("%d", &n) != EOF) {
solve();
}
return 0;
}

你可能感兴趣的:(poj)