POJ-1556 判断相交+dijkstra

33 篇文章 0 订阅
25 篇文章 0 订阅

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
解题思路:
建图时:
枚举每个墙的4点(包括一开始的(0,5)): s;
如果能到它后面的每1个墙的4点(包括(10,5)): e,
就连一条大小为dist(s,e)距离的边;
↑上一条判断需枚举这2个墙之间的每1个墙的2个线段,
如果枚举的每个墙存在1个线段与线段(s,e)相交,则判断成立;
然后dijkstra
代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;
struct POINT{
	double x,y;
	POINT(double x=0,double y=0):x(x),y(y){}
}point[20][4];
struct LINESEG{
	POINT s,e;
	LINESEG(POINT s=0,POINT e=0):s(s),e(e){}
};
double xmult(POINT sp,POINT ep,POINT op){
	return (sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
}
bool intersect(LINESEG u,LINESEG v){//线段v所在直线与线段u相交时返回true;方法:判断线段u是否跨立线段v  
 	return xmult(u.s,v.e,v.s)*xmult(v.e,u.e,v.s)>=0;
}
struct state{
	double d;
	int id;
	bool operator<(const state&t)const{
		return d>t.d;
	}
};
double dis(POINT p1,POINT p2){
	return(sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
double d[2019];
vector<pair<int,double> >edge[81];
void dijkstra(int s,int t){
	priority_queue<state>q;
	for(int i=0;i<2019;++i)d[i]=10000000;
	q.push({d[s],s});
	d[s]=0;
	while(!q.empty()){
		int now=q.top().id;q.pop();
		for(unsigned int i=0;i<edge[now].size();++i){
			int next=edge[now][i].first;
			if(d[next]>d[now]+edge[now][i].second){
				d[next]=d[now]+edge[now][i].second;
				q.push({d[next],next});
			}
		}
	}
	printf("%.2f\n",d[t]);
}int cnt,n;
void add(int x){
	for(int i=x;i<n+1;++i)for(int j=0;j<4;++j){
		for(int k=i+1;k<=n+1;++k)for(int l=0;l<4;++l){
			LINESEG v=LINESEG(point[i][j],point[k][l]);
			int ok=1;
			for(int m=i+1;m<k&&ok;++m)for(int o=0;o<4;o+=2){
				if(intersect(LINESEG(point[m][o],point[m][o+1]),v))break;
				else if(o==2)ok=0;
			}
			if(ok)edge[4*i+j].push_back(make_pair(4*k+l,dis(point[i][j],point[k][l])));
			if(k==n+1)break;
		}
		if(i==0||i==n+1)break;
	}
}
int main(){
	while(~scanf("%d",&n)&&n!=-1){
		point[0][0]=POINT(0,5),point[n+1][0]=POINT(10,5);
		for(int i=0;i<81;++i)edge[i].clear();
		for(int i=1;i<=n;++i){
			double x,y1,y2,y3,y4;scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
			point[i][0]=POINT(x,y1),point[i][1]=POINT(x,y2),
			point[i][2]=POINT(x,y3),point[i][3]=POINT(x,y4);
		}
		cnt=0;
		for(int i=0;i<n+1;++i)add(i);
		dijkstra(0,(n+1)<<2);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值