Http://poj.org/problem? Id = 1556
The Doors
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:6120 |
|
Accepted:2455 |
Description
You are to find the length of the shortest path through a chamber containing obstructing Wils. 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 wallinside the chamber, each with two doorways. the figure below tables strates such a chamber and also shows the path of minimal length.
Input
The input data for the specified strated Chamber wowould appear as follows.
2
4 2 7 8 9
7 3 4.5 6 7
The first line contains the number of interior Wils. 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 wallare in increasing order, and within each line the Y coordinates are in increasing order. the input file will contain in at least one such set of data. the end of the data comes when the number of Wils is-1.
Output
The output shoshould contain one line of output for each chamber. the line shoshould 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 shoshould contain no blanks.
Sample Input
15 4 6 7 824 2 7 8 97 3 4.5 6 7-1
Sample output
10.0010.06
Source
Mid-Central USA 1996
At the beginning, it was really a second force,
1. The function used to determine the intersection is wrong. I have determined whether it is a straight line between the source point and the end point... Second force ah ,,,
2. After the change, we will return wa, because one is missing in the judgment !,,,, No inverse ,,,
3. the vertices of the limit, such as the top and bottom sides of each wall, cannot be reached. That is to say, the two vertices cannot pass through from the source to the end, and are not excluded at the beginning, even if that is the case, you can use the AC, or the question data is too weak.
My own template for determining the intersection of straight lines:
/* ===================================================== ===============================* | The judgment point is a straight line or a straight line intersection. 1. The function value is 0, 2. Test (a, B, T1) * test (a, B, T2) <0 indicates the intersection of line AB and line t1t2 \ * ============================ =============================== */double test (point, point B, point t) {return (B. y-a.y.) * (T. x-b.x)-(B. x-a.x.) * (T. y-b.y );}
The train of thought is still relatively smooth, that is, the shortest short circuit + judgment of the intersection of straight lines
Code:
#include<cstdio>#include<cstring>#include <string>#include <map>#include <iostream>#include <cmath>using namespace std;#define INF 10000const double eps=1e-6;const int MAXN = 1011;#define Max(a,b) (a)>(b)?(a):(b)int cntp;int wn;struct Point{ Point(double x=0,double y=0):x(x),y(y){} double x,y; int id;}p[MAXN];struct Wall{ double s1,e1; double s2,e2; double s3;}w[20];//=0~~=wndouble e[MAXN][MAXN],dist[MAXN];double dis(Point a, Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}void init(){ cntp=1; for(int i=0;i<=MAXN;i++) for(int j=0;j<=MAXN;j++) { if(i == j)e[i][j]=0; else e[i][j]=INF; } p[0].x=0,p[0].y=5,p[0].id=0; for(int i=0;i<=MAXN;i++)dist[i]=INF;}double test(Point a,Point b, Point t){ return (b.y-a.y)*(t.x-b.x)-(b.x-a.x)*(t.y-b.y);}bool Judge(Point a, Point b){ if(a.id>b.id) { Point t=a; a=b; b=t; } //int flag=1; if(a.id>0) if(a.y -0.0 <=eps||10.0-a.y <=eps) return 0; if(b.id<cntp-1) if(b.y-0.0<=eps || 10.0-b.y<=eps) return 0; for(int i=a.id+1;i<b.id;i++) { Point p1(w[i].s1,w[i].e1),p2(w[i].s1,w[i].s2),p3(w[i].s1,w[i].e2),p4(w[i].s1,w[i].s3); if(!( test(a,b,p1)*test(a,b,p2)<0 || test(a,b,p3)*test(a,b,p4)<0) )return 0; } /*for(int i=a.id+1;i<b.id;i++) { if(!( (w[i].e1<5.0&&w[i].s2>5.0) || (w[i].e2<5.0&&w[i].s3>5.0) ) )return 0; }*/ return 1;}void Build(){ for(int i=0;i<cntp;i++) { for(int j=i+1;j<cntp;j++) { //if(i == j)continue; if(p[i].id == p[j].id)continue; if(Judge(p[i],p[j])) { e[i][j]=min(e[i][j],dis(p[i],p[j])); } } }}void Bellman(int v0){ int n=cntp; for(int i=0;i<cntp;i++) { dist[i]=e[v0][i]; //if(i!=v0 && dist[i]<INF) } for(int k=2;k<n;k++) { for(int u=0;u<n;u++) { if(u!=v0) { for(int j=0;j<n;j++) { if(e[j][u]!=INF && dist[j]+e[j][u]<dist[u]) { dist[u]=dist[j]+e[j][u]; } } } } }}int main(){ // freopen("poj1556.txt","r",stdin); while(~scanf("%d",&wn) && ~wn) { init(); for(int i=1;i<=wn;i++) { scanf("%lf%lf%lf%lf%lf",&w[i].s1,&w[i].e1,&w[i].s2,&w[i].e2,&w[i].s3); p[cntp].id=p[cntp+1].id=p[cntp+2].id=p[cntp+3].id=p[cntp+4].id=p[cntp+5].id=i; p[cntp].x=p[cntp+1].x=p[cntp+2].x=p[cntp+3].x=p[cntp+4].x=p[cntp+5].x=w[i].s1; p[cntp].y=0.0,p[cntp+1].y=w[i].e1,p[cntp+2].y=w[i].s2,p[cntp+3].y=w[i].e2,p[cntp+4].y=w[i].s3,p[cntp+5].y=10.0; //////////////// //e[cntp+1][cntp+2]=w[i].s2-w[i].e1; // e[cntp+3][cntp+4]=w[i].s3-w[i].e2; cntp+=6; } p[cntp].x=10.0,p[cntp].y=5.0,p[cntp].id=++wn; cntp++; //if() Build(); Bellman(0); printf("%.2lf\n",dist[cntp-1]); /////////////////// // for(int i=0;i<cntp;i++) // printf("%d %lf\n",i,dist[i]); } return 0;}