码迷,mamicode.com
首页 > 其他好文 > 详细

poj1755Triathlon(半平面交)

时间:2014-08-04 10:30:37      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   art   

链接

根据题意可以设三段路程分别为A,B,C

那么总时间t = A/V+B/U+C/W.

这样根据时间大小关系可以跟其余n-1个联立形成n-1个方程。

化简后为A(1/vj-1/vi)+B(1/uj-1/ui)+C(1/wj-1/wi)>0

这样就可以按照顺时针进行半平面交。初始需要加一个大的平面,可以加上4个点,(0,0) (0,INF) (INF,INF) (INF,0)

最后面积需》0

这个题精度要求高,在求系数的时候可以 (vi-vj)/(vi*vj) 来提高精度 ,只除一次。

bubuko.com,布布扣
  1 #include <iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<stdlib.h>
  6 #include<vector>
  7 #include<cmath>
  8 #include<queue>
  9 #include<set>
 10 using namespace std;
 11 #define N 110
 12 #define LL long long
 13 #define INF 0xfffffff
 14 const double eps = 1e-8;
 15 const double pi = acos(-1.0);
 16 const double inf = ~0u>>2;
 17 const int MAXN=1550;
 18 int m,n;
 19 double r;
 20 int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
 21 struct point
 22 {
 23     double x,y;
 24     point(double x=0,double y=0):x(x),y(y){}
 25 };
 26 struct node
 27 {
 28     int v,u,w;
 29 }ll[N];
 30 point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点
 31 void getline(point x,point y,double &a,double &b,double   &c) //两点x、y确定一条直线a、b、c为其系数
 32 {
 33     a = y.y - x.y;
 34     b = x.x - y.x;
 35     c = y.x * x.y - x.x * y.y;
 36 }
 37 void initial()
 38 {
 39     for(int i = 1; i <= m; ++i)p[i] = points[i];
 40     p[m+1] = p[1];
 41     p[0] = p[m];
 42     cCnt = m;//cCnt为最终切割得到的多边形的顶点数,将其初始化为多边形的顶点的个数
 43 }
 44 point intersect(point x,point y,double a,double b,double c) //求x、y形成的直线与已知直线a、b、c、的交点
 45 {
 46     double u = fabs(a * x.x + b * x.y + c);
 47     double v = fabs(a * y.x + b * y.y + c);
 48     point pt;
 49     pt.x=(x.x * v + y.x * u) / (u + v);
 50     pt.y=(x.y * v + y.y * u) / (u + v);
 51     return  pt;
 52 }
 53 int dcmp(double x)
 54 {
 55     if(fabs(x)<eps) return 0;
 56     return x<0?-1:1;
 57 }
 58 void cut(double a,double b ,double c)
 59 {
 60 
 61     curCnt = 0;
 62     for(int i = 1; i <= cCnt; ++i)
 63     {
 64         if(a*p[i].x + b*p[i].y + c >-eps)q[++curCnt] = p[i];// c由于精度问题,可能会偏小,所以有些点本应在右侧而没在,
 65         //故应该接着判断
 66         else
 67         {
 68             if(a*p[i-1].x + b*p[i-1].y + c > eps) //如果p[i-1]在直线的右侧的话,
 69             {
 70                 //则将p[i],p[i-1]形成的直线与已知直线的交点作为核的一个顶点(这样的话,由于精度的问题,核的面积可能会有所减少)
 71                 q[++curCnt] = intersect(p[i],p[i-1],a,b,c);
 72             }
 73             if(a*p[i+1].x + b*p[i+1].y + c > eps) //原理同上
 74             {
 75                 q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
 76             }
 77         }
 78     }
 79     for(int i = 1; i <= curCnt; ++i)p[i] = q[i];//将q中暂存的核的顶点转移到p中
 80     p[curCnt+1] = q[1];
 81     p[0] = p[curCnt];
 82     cCnt = curCnt;
 83 }
 84 void solve(int k)
 85 {
 86     //注意:默认点是顺时针,如果题目不是顺时针,规整化方向
 87     initial();
 88     for(int i = 1; i <= n; ++i)
 89     {
 90         if(i==k) continue;
 91         double a,b,c;
 92         a = (ll[k].u-ll[i].u)*1.0/(ll[k].u*ll[i].u);
 93         b = (ll[k].w-ll[i].w)*1.0/(ll[k].w*ll[i].w);
 94         c = (ll[k].v-ll[i].v)*1.0/(ll[k].v*ll[i].v);
 95         if(dcmp(a)==0&&dcmp(b)==0&&dcmp(c)<=0)
 96         {
 97             puts("No");
 98             return ;
 99         }
100         //getline(points[i],points[i+1],a,b,c);
101         cut(a,b,c);
102     }
103     /*
104       如果要向内推进r,用该部分代替上个函数
105       for(int i = 1; i <= m; ++i){
106           Point ta, tb, tt;
107           tt.x = points[i+1].y - points[i].y;
108           tt.y = points[i].x - points[i+1].x;
109           double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);
110           tt.x = tt.x * k;
111           tt.y = tt.y * k;
112           ta.x = points[i].x + tt.x;
113           ta.y = points[i].y + tt.y;
114           tb.x = points[i+1].x + tt.x;
115           tb.y = points[i+1].y + tt.y;
116           double a,b,c;
117           getline(ta,tb,a,b,c);
118           cut(a,b,c);
119       }*/
120     //多边形核的面积
121     double area = 0;
122     for(int i = 1; i <= cCnt; ++i)
123         area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y;
124     area = fabs(area / 2.0);
125     if(dcmp(area)>0)
126     printf("Yes\n");
127     else
128     puts("No");
129 
130 }
131 /*void GuiZhengHua(){
132      //规整化方向,逆时针变顺时针,顺时针变逆时针
133     for(int i = 1; i < (m+1)/2; i ++)
134       swap(points[i], points[m-i]);
135 }*/
136 int main()
137 {
138     points[1] = point(0,0);
139     points[2] = point(INF,0);
140     points[3] = point(INF,INF);
141     points[4] = point(0,INF);
142     points[5] = points[1];
143     m = 4;
144     int i;
145     while(scanf("%d",&n)!=EOF)
146     {
147         for(i = 1; i <=n ;i++)
148         scanf("%d%d%d",&ll[i].v,&ll[i].u,&ll[i].w);
149         for(i = 1;i <= n ;i++)
150         solve(i);
151     }
152     return 0;
153 }
View Code

 

poj1755Triathlon(半平面交),布布扣,bubuko.com

poj1755Triathlon(半平面交)

标签:style   blog   http   color   os   io   for   art   

原文地址:http://www.cnblogs.com/shangyu/p/3889299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!