poj1556 The Doors
http://www.elijahqi.win/archives/1117
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.
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
这里用了叉乘判断相交的解法,本来死活过不去,后来随便改改等于号什么的就过了。。对这个叉乘什么的还不太会,留坑待填,另外%lf 在Poj上g++会有问题
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define N 110
using namespace std;
struct node{
int y,next;double z;
}data[55000];
struct pp
{
double x,y;
}point[110];
struct lli
{
pp pa,pb;
}line[110];
int h[N<<2],num,cnt,m;
inline void cl(double x1,double y1,double x2,double y2){line[++m].pa.x=x1;line[m].pa.y=y1;line[m].pb.x=x2;line[m].pb.y=y2;}
inline double calc(pp a,pp b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline void insert1(int x,int y,double z){
data[++cnt].y=y;data[cnt].next=h[x];data[cnt].z=z;h[x]=cnt;
data[++cnt].y=x;data[cnt].next=h[y];data[cnt].z=z;h[y]=cnt;
}
inline double multiply(pp a,pp b,pp c){
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
inline bool cross(lli line1,lli line2){
pp aa=line1.pa,bb=line1.pb,cc=line2.pa,dd=line2.pb;
if (max(aa.x,bb.x)<min(cc.x,dd.x)) return false;
if (max(aa.y,bb.y)<min(cc.y,dd.y)) return false;
if (max(cc.x,dd.x)<min(aa.x,bb.x)) return false;
if (max(cc.y,dd.y)<min(aa.y,bb.y)) return false;
if (multiply(cc,bb,aa)*multiply(bb,dd,aa)<=0) return false;
if (multiply(aa,dd,cc)*multiply(dd,bb,cc)<=0) return false;
return true;
}
inline bool judge(pp a,pp b){
lli line1;line1.pa=a;line1.pb=b;
for (int i=1;i<=m;++i)
if (cross(line[i],line1)) return false;
return true;
}
bool flag[N<<2];
double f[N<<2];
inline void spfa(){
queue<int> q;memset(flag,0,sizeof(flag));flag[1]=true;q.push(1);memset(f,0x7f,sizeof(f));f[1]=0;
while (!q.empty()){
int x=q.front();q.pop();flag[x]=false;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;double z=data[i].z;
if (f[x]+z<f[y]) {
f[y]=f[x]+z;if (flag[y]==false){
q.push(y);flag[y]=true;
}
}
}
}
}
int n;double x,y_1,y_2,y_3,y_4;
int main(){
freopen("poj1556.in","r",stdin);
while (1){
scanf("%d",&n);if (n==-1) break;
memset(h,0,sizeof(h));m=num=cnt=0;
point[++num].x=0;point[num].y=5;
for (int i=1;i<=n;++i){
scanf("%lf%lf%lf%lf%lf",&x,&y_1,&y_2,&y_3,&y_4);
point[++num].x=x;point[num].y=y_1;point[++num].x=x;point[num].y=y_2;
point[++num].x=x;point[num].y=y_3;point[++num].x=x;point[num].y=y_4;
cl(x,0,x,y_1);cl(x,y_2,x,y_3);cl(x,y_4,x,10);
}point[++num].x=10;point[num].y=5;
//for (int i=1;i<=m;++i) printf("%lf %lf %lf %lf\n",line[i].pa.x,line[i].pa.y,line[i].pb.x,line[i].pb.y);
//for (int i=1;i<=num;++i) printf("%lf %lf\n",point[i].x,point[i].y);
for (int i=1;i<=num;++i)
for (int j=i+1;j<=num;++j){
if (judge(point[i],point[j])) insert1(i,j,calc(point[i],point[j]));
}
//for (int i=1;i<=cnt;++i) printf("%d %lf\n",data[i].y,data[i].z);
spfa();
printf("%.2lf\n",f[num]);
}
return 0;
}