POJ1556 The Doors [线段相交 DP]
The Doors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8334 | Accepted: 3218 |
Description
You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length.
Input
The input data for the illustrated chamber would appear as follows.
2
4 2 7 8 9
7 3 4.5 6 7
The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.
2
4 2 7 8 9
7 3 4.5 6 7
The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.
Output
The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.
Sample Input
1 5 4 6 7 8 2 4 2 7 8 9 7 3 4.5 6 7 -1
Sample Output
10.00 10.06
Source
题意:从(0,5)走到(10,5)最短路
我太傻逼了,查了好长时间计算几何的错,结果是求DAG的DP忘清空vis了
线段相交做两个直线与线段相交就行了
注意本题一个端点在另一条线上不能算相交哦
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> using namespace std; typedef long long ll; const int N=205,M=1e4+5; const double INF=1e9; const double eps=1e-8; inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f; } inline int sgn(double x){ if(abs(x)<eps) return 0; else return x<0?-1:1; } struct Vector{ double x,y; Vector(double a=0,double b=0):x(a),y(b){} bool operator <(const Vector &a)const{ return x<a.x||(x==a.x&&y<a.y); } void print(){ printf("%lf %lf\n",x,y); } }; typedef Vector Point; Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);} Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);} Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);} Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);} bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;} double Cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } double DisPP(Point a,Point b){ Point t=a-b; return sqrt(t.x*t.x+t.y*t.y); } struct Line{ Point s,t; Line(){} Line(Point p,Point v):s(p),t(v){} }l[N]; int cl; bool isLSI(Line l1,Line l2){ Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s; return sgn(Cross(v,u))!=sgn(Cross(v,w))&&sgn(Cross(v,u))!=0&&sgn(Cross(v,w))!=0; } bool isSSI(Line l1,Line l2){ return isLSI(l1,l2)&&isLSI(l2,l1); } bool can(Point a,Point b){ Line line(a,b); for(int i=1;i<=cl;i++) if(isSSI(l[i],line)) return false; return true; } int n,s,t; struct edge{ int v,ne; double w; }e[M<<1]; int h[N],cnt=0; inline void ins(int u,int v,double w){//printf("ins %d %d %lf\n",u,v,w); cnt++; e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt; } double d[N]; int vis[N]; double dp(int u){ if(vis[u]) return d[u]; vis[u]=1; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; d[u]=min(d[u],dp(v)+e[i].w); } return d[u]; } void DAG(){ for(int i=s;i<=t;i++) d[i]=INF; memset(vis,0,sizeof(vis)); d[t]=0;vis[t]=1; dp(s); } Point p[N][5]; Point S(0,5),T(10,5); inline int idx(int u){return u%4==0?u/4:u/4+1;} inline int idy(int u){return u%4==0?4:u%4;} double x; int main(int argc, const char * argv[]) { while(true){ n=read();s=0;t=4*n+1; if(n==-1) break; cnt=0;memset(h,0,sizeof(h)); cl=0; for(int i=1;i<=n;i++){ scanf("%lf%lf%lf%lf%lf",&x,&p[i][1].y,&p[i][2].y,&p[i][3].y,&p[i][4].y); p[i][1].x=p[i][2].x=p[i][3].x=p[i][4].x=x; int num=(i-1)*4; //for(int j=1;j<=4;j++) p[i][j].print(); if(i==1){ for(int j=1;j<=4;j++) ins(s,num+j,DisPP(S,p[i][j])); }else{ for(int j=1;j<=4;j++){ for(int u=1;u<=num;u++){ if(can(p[idx(u)][idy(u)],p[i][j])) ins(u,num+j,DisPP(p[idx(u)][idy(u)],p[i][j])); } if(can(S,p[i][j])) ins(s,num+j,DisPP(S,p[i][j])); } } l[++cl]=Line(Point(x,0),p[i][1]); l[++cl]=Line(p[i][2],p[i][3]); l[++cl]=Line(p[i][4],Point(x,10)); } int num=n*4; for(int u=1;u<=num;u++) if(can(p[idx(u)][idy(u)],T)) ins(u,t,DisPP(p[idx(u)][idy(u)],T)); if(can(S,T)) {puts("10.00");continue;} DAG(); printf("%.2f\n",d[s]); } return 0; }
Copyright:http://www.cnblogs.com/candy99/