poj 1556 The Doors(最短路+几何)
2017-03-31 15:53
387 查看
题意:有一个小屋,起点在(0,5),重点在(10,5),中间有0—18堵墙,每堵墙上有两个门,求起点到终点最短路
思路:起点和终点和门的端点建图求最短路,在求两个端点之间的距离(直线距离)的时候要判断两个端点之间是否有墙隔着
思路:起点和终点和门的端点建图求最短路,在求两个端点之间的距离(直线距离)的时候要判断两个端点之间是否有墙隔着
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <iomanip> #include <cmath> using namespace std; const int MAXN = 150; const double INF = 9999999; const double precision = 1e-8; struct Point { double x,y; Point(){} Point(double xx, double yy):x(xx),y(yy){} }; struct Line { Point sp,ep; Line(){} Line(Point a, Point b):sp(a),ep(b){} }; int n,pcnt,lcnt; Point ps[MAXN]; Line ls[MAXN]; double g[MAXN][MAXN]; double dis[MAXN]; int book[MAXN]; double dist(Point a, Point b) { return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); } int dblcmp(double d) { if(fabs(d) < precision) return 0; return d > 0 ? 1 : -1; } double det(Point a, Point b, Point c) { //b.x-a.x,b.y-a.y //c.x-a.x,c.y-a.y return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y); } bool intersect(Point a, Point b, Line p) { int d1 = dblcmp(det(a,b,p.sp)); int d2 = dblcmp(det(a,b,p.ep)); int d3 = dblcmp(det(p.sp,p.ep,a)); int d4 = dblcmp(det(p.sp,p.ep,b)); if(d1*d2 < 0 && d3*d4 < 0) return true; return false; } bool check(int i, int j) { for(int k = 0; k < lcnt; ++k) { if(intersect(ps[i],ps[j],ls[k])) return false; } return true; } double dijkstra() { memset(book,0,sizeof(book)); for(int i = 1; i <= pcnt; ++i) dis[i] = g[1][i]; book[1] = 1; double minn; int u,v; for(int i = 1; i <= pcnt-1; ++i) { minn = INF; for(int j = 1; j <= pcnt; ++j) { if(book[j] == 0 && dis[j] < minn) { minn = dis[j]; u = j; } } book[u] = 1; for(v = 1; v <= pcnt; ++v) { if(dis[v] > dis[u] + g[u][v]) dis[v] = dis[u] + g[u][v]; } } return dis[pcnt]; } int main() { double x,y; while(scanf("%d",&n) && n != -1) { pcnt = 1; lcnt = 0; memset(g,0,sizeof(g)); ps[pcnt++] = Point(0,5); for(int i = 0; i < n; ++i) { cin >> x; for(int k = 0; k < 4; ++k) { cin >> y; ps[pcnt++] = Point(x,y); } ls[lcnt++] = Line(Point(x,0),ps[pcnt-4]); ls[lcnt++] = Line(ps[pcnt-3],ps[pcnt-2]); ls[lcnt++] = Line(ps[pcnt-1],Point(x,10)); } ps[pcnt] = Point(10,5); for(int i = 1; i <= pcnt; ++i) { for(int j = 1; j <= pcnt; ++j) { if(i == j) g[i][j] = 0; else if(check(i,j)) g[i][j] = dist(ps[i],ps[j]); else g[i][j] = INF; } } double res = dijkstra(); // for(int k = 1; k <= pcnt; ++k) // { // for(int i = 1; i <= pcnt; ++i) // { // for(int j = 1; j <= pcnt; ++j) // if(g[i][k]+g[k][j] < g[i][j]) // g[i][j] = g[i][k] + g[k][j]; // } // } cout << fixed << setprecision(2) << res << endl; } return 0; }
相关文章推荐
- POJ 1556 The Doors --几何,最短路
- POJ 1556 The Doors (计算几何判断线段相交+最短路)
- POJ1556——The Doors 计算几何,最短路
- POJ 1556 The Doors --几何,最短路
- Poj 1556 The Doors 计算几何+最短路
- POJ 1556 The Doors(计算几何+最短路)
- POJ 1556 The Doors(几何+最短路)
- ●POJ 1556 The Doors(简单计算几何+最短路)
- POJ 1556 The Doors(计算几何+最短路)
- POJ 1556 The Doors 计算几何+ 最短路
- 【POJ】1556 The Doors(计算几何基础+spfa)
- POJ-1556 The Doors 线段相交+最短路
- POJ 1556 The Doors(Dijkstra+计算几何)
- poj 1556 The Doors(线段交+最短路)
- poj 1556 The Doors (线段之间位置的判断+最短路)
- poj 1556 The Doors(最短路+判断线段相交)
- POJ 1556 计算几何+最短路
- POJ 1556 The Doors 判断线段相交+最短路
- poj 1556 The Doors 线段相交判断+最短路
- poj 1556 计算几何+最短路