POJ 1566(toj 1952)
来源:互联网 发布:windows怎么卸载软件 编辑:程序博客网 时间:2024/05/22 03:53
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 126 Accepted Runs: 69
The Problem
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.
24 2 7 8 97 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 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
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
交题的时候poj挂了,在hdu上也没找到,突然想起自己学校的oj,在toj上一找还真找到了,交了一发,1A,开心~
说明toj还是很厉害的,题目还是很全的~
思路:乍一看感觉是一道几何题,再一看发现可以用最短路做
在所有点之间连线,判断跟墙是否相交,不相交的话就建边,然后跑一边最短路,什么方法都行了
#include <iostream>#include <cstring>#include <queue>#include <cmath>#include <iomanip>#define INF 100000000.0using namespace std;struct point{ double x,y; point(){}; point (double _x,double _y) { x=_x,y=_y; }};typedef point vec;double operator /(vec a, vec b){ return (a.x*b.y-a.y*b.x);}vec operator- (point a,point b){ vec c; c.x=a.x-b.x; c.y=a.y-b.y; return c;}struct line{ point a,b; line(){}; line (point _a,point _b) { a=_a; b=_b; }};double dis(point a,point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}struct edge{ int v,next; double w;};int cross(line m,line n){ if(((n.a-m.a)/(m.b-m.a))*((n.b-m.a)/(m.b-m.a))<=0 &&((m.a-n.a)/(n.b-n.a))*((m.b-n.a)/(n.b-n.a))<=0) return 1; else return 0;}edge e[5005];int head[105],cnt=0,vis[5005],n,m;double dist[105];queue<int>q;void addedge(int u,int v,double w){ e[cnt].v=v; e[cnt].w=w; e[cnt].next=head[u]; head[u]=cnt++;}void init(){ memset(head,-1,sizeof(head)); cnt=0;}void spfa(int s){ for(int i=0;i<=n;i++) dist[i]=INF,vis[i]=0; while(!q.empty()) q.pop(); q.push(s); vis[s]=1; dist[s]=0.0; while(!q.empty()) { int tmp; tmp=q.front(); q.pop(); for(int i=head[tmp];i!=-1;i=e[i].next) { if(dist[e[i].v]>dist[tmp]+e[i].w) { dist[e[i].v]=dist[tmp]+e[i].w; if(!vis[e[i].v]) { vis[e[i].v]=1; q.push(e[i].v); } } } vis[tmp]=0; }}point p[105];line le[5005];bool check(int i,int j){ line temp(p[i],p[j]); int left,right; if(i!=0 && j!=n-1) { left=(((i-1)/4)+1)*3; right=((j-1)/4)*3; for(int c1=left;c1<right;c1++) { if(cross(temp,le[c1])) { return false; } } } else if(i==0 && j!=n-1) { right=((j-1)/4)*3; for(int c1=0;c1<right;c1++) { if(cross(temp,le[c1])) { return false; } } } else if(i!=0 && j==n-1) { left=(((i-1)/4)+1)*3; for(int c1=left;c1<m-1;c1++) { if(cross(temp,le[c1])) { return false; } } } else { for(int c1=0;c1<m-1;c1++) { if(cross(temp,le[c1])) { return false; } } } return true;}int main(){ int t; while(cin>>t) { if(t==-1) break; init(); int totl=0,totp=1; n=4*t+2; m=3*t; p[0].x=0,p[0].y=5.0; p[n-1].x=10.0,p[n-1].y=5.0; for(int i=0;i<t;i++) { double x,y1,y2,y3,y4; point a,b; line tmp; cin>>x>>y1>>y2>>y3>>y4; p[totp].x=x,p[totp++].y=y1; p[totp].x=x,p[totp++].y=y2; p[totp].x=x,p[totp++].y=y3; p[totp].x=x,p[totp++].y=y4; a.x=x,a.y=0.0; b.x=x,b.y=y1; tmp.a=a,tmp.b=b; le[totl++]=tmp; a.x=x,a.y=y2; b.x=x,b.y=y3; tmp.a=a,tmp.b=b; le[totl++]=tmp; a.x=x,a.y=y4; b.x=x,b.y=10.0; tmp.a=a,tmp.b=b; le[totl++]=tmp; } for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(check(i,j)) addedge(i,j,dis(p[i],p[j])); } } spfa(0); double ans=dist[n-1]; cout<<fixed<<setprecision(2)<<ans<<endl; } return 0;}
- POJ 1566(toj 1952)
- Toj 1116 Poj 1324(A*搜索) Holedox Moving
- 【TOJ】2299【POJ】2117.Electricity
- TOJ 3445 POJ 2192 Zipper
- TOJ 1398 POJ 2362 Square DFS
- Stock Exchange POJ 3903/ TOJ 3120
- TOJ 1752 POJ 2255 Tree Recovery
- next_permutation toj,poj 上习题小结
- TOJ 1702 POJ 3272 Cow Traffic
- TOJ 2018 POJ 1523 SPF / 割点
- TOJ 2896.Antimonotonicity(贪心)
- TOJ 1926 POJ 1548 Robots 贪心 水题 C语言
- TOJ 2273 POJ 2818 Making Change DFS C语言
- TOJ 1115 POJ 1328 Radar Installation 贪心 C/C++
- POJ 2488 DFS+记录路径 TOJ 3520 BFS+记录路径
- TOJ 2380 POJ 2536 Gopher II /二分图
- TOJ 1112 POJ 2939 Flavius Josephus Reloaded/哈希链求地址法
- TOJ 1883 ZOJ 1298 POJ 1135 Domino Effect / Dijkstra
- C#的JSON操作工具类
- linux_服务
- js中方法调用加括号和不加括号的区别
- 操作activiti出现的bug
- 打包时间统计功能优化
- POJ 1566(toj 1952)
- 修改静态库输出路径
- 如何在 Linux 上安装服务器程序?
- jfinal急速demo的坑!!!很多人会看过我转载的这篇文章,但是这个其实是有bug的!!!
- Java过滤器模式(条件模式)
- linux安装redis遇到过的一些坑
- ubuntu64位,出现hadoop2.6部署警告: Unable to load native-hadoop library
- 搬家了搬家了!
- PE文件结构详解(一)基本概念