POJ1556 The Doors [线段相交 DP]
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8334 | Accepted: 3218 |
Description

Input
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
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=,M=1e4+;
const double INF=1e9;
const double eps=1e-;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
inline int sgn(double x){
if(abs(x)<eps) return ;
else return x<?-:;
}
struct Vector{
double x,y;
Vector(double a=,double b=):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)==&&sgn(a.y-b.y)==;} 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))!=&&sgn(Cross(v,w))!=;
}
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=;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<<];
int h[N],cnt=;
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]=;
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,,sizeof(vis));
d[t]=;vis[t]=;
dp(s);
} Point p[N][];
Point S(,),T(,);
inline int idx(int u){return u%==?u/:u/+;}
inline int idy(int u){return u%==?:u%;}
double x;
int main(int argc, const char * argv[]) {
while(true){
n=read();s=;t=*n+;
if(n==-) break;
cnt=;memset(h,,sizeof(h));
cl=; for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf%lf",&x,&p[i][].y,&p[i][].y,&p[i][].y,&p[i][].y);
p[i][].x=p[i][].x=p[i][].x=p[i][].x=x;
int num=(i-)*;
//for(int j=1;j<=4;j++) p[i][j].print();
if(i==){
for(int j=;j<=;j++)
ins(s,num+j,DisPP(S,p[i][j]));
}else{
for(int j=;j<=;j++){
for(int u=;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,),p[i][]);
l[++cl]=Line(p[i][],p[i][]);
l[++cl]=Line(p[i][],Point(x,));
}
int num=n*;
for(int u=;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 ;
}
POJ1556 The Doors [线段相交 DP]的更多相关文章
- POJ1556 最短路 + 线段相交问题
POJ1556 题目大意:比较明显的题目,在一个房间中有几堵墙,直着走,问你从(0,5)到(10,5)的最短路是多少 求最短路问题,唯一变化的就是边的获取,需要我们获取边,这就需要判断我们想要走的这条 ...
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
- POJ 1066 Treasure Hunt (线段相交)
题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...
- 简单几何(线段相交) POJ 1066 Treasure Hunt
题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 ...
- poj 1066 线段相交
链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- 线段相交 poj 1066
// 线段相交 poj 1066 // 思路:直接枚举每个端点和终点连成线段,判断和剩下的线段相交个数 // #include <bits/stdc++.h> #include <i ...
随机推荐
- 2017年 JavaScript 框架回顾 -- React生态系统
前一篇文章中,我们介绍了2017年 JavaScript 框架的整体情况.我们也了解到在众多的前端框架中,目前最为庞大又在快速增长的当属 React 了,本文就来重点介绍 React 的生态系统. 首 ...
- Laravel 验证中文本地化
1.使用bootsrap好看的提示样式 但是会提示英文 2.将提示中文本地化 2.1.在/resouce/lang下创建文件夹:zh 2.2.已经有小伙伴做好了翻译 https://gist.gith ...
- API接口签名验证2
http://www.jianshu.com/p/d47da77b6419 系统从外部获取数据时,通常采用API接口调用的方式来实现.请求方和�接口提供方之间的通信过程,有这几个问题需要考虑: 1.请 ...
- 从数据库、代码和服务器对PHP网站Mysql做性能优化
数据库优化是PHP面试几乎都会被问到的事情,也是我们工作中应该注意的事情,当然,如果是小网站无所谓优化不优化,网站访问量大了自然会暴漏数据库的瓶颈,这个瓶颈是各方面问题综合导致的,下面我们来做下数据库 ...
- Core Graphics框架是Quartz的核心,也是内容描画的基本接口。
Core Graphics框架是Quartz的核心,也是内容描画的基本接口.
- Java 对二值化图片识别连通域
用Java 对 已经 二值化了的图片 标记连通域 每块的连通域都标记不一样的数字 public static void main(String [] args) throws IOException ...
- 经典CSS坑:如何完美实现垂直水平居中?
经典CSS坑:如何完美实现垂直水平居中? 踩了各种坑之后总结出的方法,开门见山,直接上代码和效果图.希望对读者有所帮助,后续如果有更好的方法,我也会持续更新 第一种方法:transform(个人认为最 ...
- git学习网址
git的学习网址:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/
- 记一些安卓app反编译修改的记录
2017-12-2209:00:40 好几天没有写过博客了,因为马上要期末考试,只能暂且放下我的小玩物,专心复习我的期末考试. 今天突然想设置一个安卓的栏目,记录下自己从高中就爱玩的一些东西,像刷机呀 ...
- 安装python的注意事项
第一步: 下载安装文件 python-3.5.2.exe 第二步: 点击安装,下一步继续 第三步: 配置环境变量,记住第一次安装时的路径. 第四步: 测试,打开CMD,输入python.若未找到命令 ...