欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poj1556 The Doors

程序员文章站 2022-04-02 16:57:18
...
The Doors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7070   Accepted: 2791

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. 
poj1556 The Doors

Input

The input data for the illustrated chamber would appear as follows. 


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

输出:

10.00

10.06


题解:

其实就是建图+最短路。每个门的两个点与其他点可以直达的话就建边,边权为他们的距离,然后跑最短路。(表示我不知道为什么这样是对的)


代码(网上只有第一个人的是对的):

#include<cstring>  
#include<algorithm>  
#include<cmath>  
#include<iostream>  
#include<cstdio>  
#include<vector>  
#define N 1010  
#define inf 99999999999.0  
  
using namespace std;  
  
struct Point {  
    double x,y;  
    int id;  
} point;  
  
struct Line {  
    Point d,u;  
    int th;  
};  
  
int n,pnum,pline;  
vector<Line>vec;  
vector<Point>p;  
double mp[N][N];  
  
void add_Line(Point a,Point b) {  
    Line it;  
    it.d=a;  
    it.u=b;  
    it.th=pline++;  
    vec.push_back(it);  
}  
  
///叉积  
double multi(Point p0,Point p1,Point p2) {  
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);  
}  
  
///线段相交判断  
bool is_inter(Point s1,Point e1,Point s2,Point e2) {  
    return (max(s1.x,e1.x)>=min(s2.x,e2.x))&&  
           (max(s2.x,e2.x)>=min(s1.x,e1.x))&&  
           (max(s1.y,e1.y)>=min(s2.y,e2.y))&&  
           (max(s2.y,e2.y)>=min(s1.y,e1.y))&&  
           (multi(s1,s2,e1)*multi(s1,e1,e2)>0)&&  
           (multi(s2,s1,e2)*multi(s2,e2,e1)>0);  
}  
  
double dist(Point a,Point b) {  
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));  
}  
  
//建图  
void init() {  
    for(int i=0; i<=pnum; i++) {  
        for(int j=i; j<=pnum; j++) {  
            if(i==j) {  
                mp[i][j]=mp[j][i]=0.0;  
                continue;  
            }  
            bool flag=1;  
            Point s1,e1;  
            s1=p[i],e1=p[j];  
            int l=(i+1)/2,r=(j+1)/2;  
            for(int k=l+1; k<r; k++) {  
                if(is_inter(s1,e1,vec[k].d,vec[k].u)) {///有一条边相交  
                    flag=0;  
                    break;  
                }  
            }  
            if(flag)mp[i][j]=mp[j][i]=dist(s1,e1);  
            else    mp[i][j]=mp[j][i]=inf;  
        }  
    }  
}  
  
bool vis[N];  
int pre[N];  
double d[N];  
  
void Dijkstra(int beg) {  
    for(int i=0; i<=pnum; i++) {  
        d[i]=inf;  
        vis[i]=false;  
    }  
    d[beg]=0;  
    for(int j=0; j<=pnum; j++) {  
        int k=-1;  
        int Min=inf;  
        for(int i=0; i<=pnum; i++)  
            if(!vis[i]&&d[i]<Min) {  
                Min=d[i];  
                k=i;  
            }  
        if(k==-1)break;  
        vis[k]=true;  
        for(int i=0; i<=pnum; i++)  
            if(!vis[i]&&d[k]+mp[k][i]<d[i]) {  
                d[i]=d[k]+mp[k][i];  
            }  
    }  
}  
int main() {  
    while(cin>>n) {  
        if(n==-1)break;  
        vec.clear();  
        p.clear();  
        double x,y11,y2,y3,y4;  
        double y5=10.0;  
        pnum=0;  
        pline=0;  
        Point a,b;  
        a.x=0,a.y=5.0,a.id=pnum++;  
        p.push_back(a);  
        for(int i=0; i<n; i++) {  
            scanf("%lf%lf%lf%lf%lf",&x,&y11,&y2,&y3,&y4);  
            a.x=x;  
            b.x=x;  
            a.y=0,b.y=y11,a.id=pnum++,b.id=pnum++;  
            add_Line(a,b);  
            p.push_back(a);  
            p.push_back(b);  
            a.y=y2,b.y=y3,a.id=pnum++,b.id=pnum++;  
            add_Line(a,b);  
            p.push_back(a);  
            p.push_back(b);  
            a.y=y4,b.y=y5,a.id=pnum++,b.id=pnum++;  
            add_Line(a,b);  
            p.push_back(a);  
            p.push_back(b);  
        }  
        a.x=10.0,a.y=5.0,a.id=pnum;  
        p.push_back(a);  
        init();  
        Dijkstra(0);  
        printf("%.2f\n",d[pnum]);  
    }  
    return 0;  
}  


本人蜜汁错误代码:

#include<cstring>  
#include<algorithm>  
#include<cmath>  
#include<iostream>  
#include<cstdio>  
#include<vector>  
using namespace std;
struct aaa{
	double x,y;
}a[100001],b[100001],c[100001];
int n,m,tot,flag,i,j,ii;
double xxx,yy1,yy2,yy3,yy4,f[1001][1001];
double direction(aaa pi,aaa pj,aaa pk){  
    return(pj.x-pi.x)*(pk.y-pi.y)-(pk.x-pi.x)*(pj.y-pi.y);  
}  
int on_segment(aaa pi,aaa pj,aaa pk){  
    if((min(pi.x,pj.x)<=pk.x)&&(max(pi.x,pj.x)>=pk.x)&&(min(pi.y,pj.y)<=pk.y)&&(max(pi.y,pj.y)>=pk.y))return 1;  
    return 0;  
}  
int pd(aaa p1,aaa p2,aaa p3,aaa p4){  
    int rr=0;  
    if(((max(p1.x,p2.x)>=min(p3.x,p4.x))&&(min(p1.x,p2.x)<=max(p3.x,p4.x))  
     &&(max(p1.y,p2.y)>=min(p3.y,p4.y))&&(min(p1.y,p2.y)<=max(p3.y,p4.y)))){  
         rr=1;  
     }  
      else return 0;  
    double d1=direction(p3,p4,p1),d2=direction(p3,p4,p2),d3=direction(p1,p2,p3),d4=direction(p1,p2,p4);  
    if(((d1>0&&d2<0)||(d1<0&&d2>0))&&((d3>0&&d4<0)||(d3<0&&d4>0)))return 1;  
    if(d1==0&&on_segment(p3,p4,p1))return 1;  
    if(d2==0&&on_segment(p3,p4,p2))return 1;  
    if(d3==0&&on_segment(p1,p2,p3))return 1;  
    if(d4==0&&on_segment(p1,p2,p4))return 1;  
    return 0;  
}  
int main(){
	while(1){
		scanf("%d",&n);
		if(n==-1)return 0;
		m=1;
		c[m].x=0.0;c[m].y=5.0;
		for(i=1;i<=n;i++){
			scanf("%lf%lf%lf%lf%lf",&xxx,&yy1,&yy2,&yy3,&yy4);
			tot++;a[tot].x=b[tot].x=xxx;a[tot].y=0;b[tot].y=yy1;
			tot++;a[tot].x=b[tot].x=xxx;a[tot].y=yy2;b[tot].y=yy3;
			tot++;a[tot].x=b[tot].x=xxx;a[tot].y=yy4;b[tot].y=10;
			m++;c[m].x=xxx;c[m].y=yy1;
			m++;c[m].x=xxx;c[m].y=yy2;
			m++;c[m].x=xxx;c[m].y=yy3;
			m++;c[m].x=xxx;c[m].y=yy4;
		}
	m++;
	for(i=1;i<=m;i++)
	 for(j=1;j<=m;j++)f[i][j]=10000.0; 
	c[m].x=10.0;c[m].y=5.0;
	for(i=1;i<m;i++)
	 for(j=i+1;j<=m;j++)
	  if(c[i].x<c[j].x){
	  	flag=0;
	  	for(ii=1;ii<=tot;ii++)
	  	 if(b[ii].x>c[i].x&&b[ii].x<c[j].x){
	  	 	if(pd(c[i],c[j],a[ii],b[ii])){
	  	 		flag=1;
	  	 		break;
	  	 	}
	  	 }
	  	 if(!flag){
	  	 	double t=(c[j].x-c[i].x),k=(c[j].y-c[i].y);
	  	 	t*=t;k*=k;
	  	 	f[i][j]=f[j][i]=sqrt(t+k);
	  	 }
	  }
	  for(ii=1;ii<=m;ii++)
	   for(i=1;i<=m;i++)
	    for(j=1;j<=m;j++)
	     if(i!=ii&&i!=j&&ii!=j)
	      f[i][j]=min(f[i][j],f[i][ii]+f[ii][j]);
	  printf("%.2lf\n",f[1][m]);  
}
}