poj 1556 The Doors
来源:互联网 发布:法国武器知乎 编辑:程序博客网 时间:2024/05/15 23:50
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <iomanip>using namespace std;const int INF = 100000000;const int MAXN = 100;struct Point{ double x; double y;};struct Edge{ int u; int v;};int n;double WallX[20];Point pDoor[MAXN];int pNum;double pY[20][4];double g[MAXN][MAXN];Edge edges[MAXN * MAXN];int eNum;int i, j;double Dis(Point a, Point b){ return sqrt( (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) );}double Judge(double x1, double y1, double x2, double y2, double x3, double y3){ return ((y3 - y1) * (x2 - x1) - (y2 - y1) * (x3 - x1));}bool CanConnect(Point a, Point b){ if(a.x >= b.x) return false; bool flag = true; int i = 0; while(WallX[i] <= a.x && i < n) i++; while(WallX[i] < b.x && i < n) { if(Judge(a.x, a.y, b.x, b.y, WallX[i], 0) * Judge(a.x, a.y, b.x, b.y, WallX[i], pY[i][0]) < 0 || Judge(a.x, a.y, b.x, b.y, WallX[i], pY[i][1]) * Judge(a.x, a.y, b.x, b.y, WallX[i], pY[i][2]) < 0 || Judge(a.x, a.y, b.x, b.y, WallX[i], pY[i][3]) * Judge(a.x, a.y, b.x, b.y, WallX[i], 10) < 0 ) { flag = false; break; } i++; } return flag;}double Bellman_Ford(int Start, int End){ double dist[MAXN]; int i, j; for(i = 0; i < MAXN; ++i) dist[i] = INF; dist[Start] = 0; bool flag = true; for(i = 0; i < pNum && flag; ++i) { flag = false; for(j = 0; j < eNum; ++j) { if(dist[edges[j].u] < INF && dist[edges[j].v] > dist[edges[j].u] + g[edges[j].u][edges[j].v]) { dist[edges[j].v] = dist[edges[j].u] + g[edges[j].u][edges[j].v]; flag = true; } } } return dist[End];}void ReadCase(){ int i, j; pDoor[0].x = 0; pDoor[0].y = 5; pNum = 1; for(i = 0; i < n; ++i) { cin>>WallX[i]; for(j = 0; j < 4; ++j) { pDoor[pNum].x = WallX[i]; cin >> pDoor[pNum].y; pY[i][j] = pDoor[pNum].y; pNum++; } } pDoor[pNum].x = 10; pDoor[pNum].y = 5; pNum++; for(i = 0; i < pNum; ++i) { for(j = 0; j < pNum; ++j) { g[i][j] = INF; } } eNum = 0; for(i = 0; i < pNum; ++i) { for(j = i+1; j < pNum; ++j) { if(CanConnect(pDoor[i], pDoor[j])) { g[i][j] = Dis(pDoor[i], pDoor[j]); edges[eNum].u = i; edges[eNum].v = j; eNum++; } } } cout<<setiosflags(ios::fixed)<<setprecision(2)<<Bellman_Ford(0, pNum-1)<<endl;}int main(){ while(cin>>n) { if(n == -1) break; ReadCase(); } return 0;}
0 0
- 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
- poj 1556 The Doors
- poj 1556 The Doors
- POJ 1556 The Doors
- poj 1556The Doors
- POJ 1556 The Doors
- poj 1556 The Doors
- POJ 1556 The Doors 笔记
- zoj 1721 || poj 1556 The Doors
- POJ 1556 The Doors(Dijkstra+计算几何)
- POJ 1556 The Doors (线段相交+Dijkstra)
- 《鸟哥的linux私房菜》学习笔记
- C# 调用exe 传递参数
- ZOJ 3736 Pocket Cube
- 祈禱의 原理 (韓長庚 易學原理總論)
- 《深入理解计算机系统》优化程序性能
- poj 1556 The Doors
- 跟着《算法导论》学习——数据结构之二叉搜索树
- postgresql进程
- ubuntu 下安装 flash player plugin
- linux 文件及其权限
- 开放数据集合分享
- 整理一些Delphi关于操作文件的函数
- 又是一年,我的2013年终总结
- opencv学习(5) 使用迭代器遍历图像的像素