poj1556 && nyoj227
2016-09-02 20:38
155 查看
题目链接poj1556 && nyoj227
题意:从(0,5)点到(10,5)点的最近距离。
思路:可以转化成从起点到终点的最短路,这题放了好几天了,一直想不到怎么建图,今天终于想出来了。
考察线段相交。
AC代码:
题意:从(0,5)点到(10,5)点的最近距离。
思路:可以转化成从起点到终点的最短路,这题放了好几天了,一直想不到怎么建图,今天终于想出来了。
考察线段相交。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <stack> #include <queue> #include <vector> #include <cmath> #include <map> #include <set> #define ll long long #define llu unsigned long long using namespace std; const double eps = 1e-6; struct Point { double x,y; Point (double x = 0,double y = 0) : x(x),y(y) {} }; Point p[150]; struct Line{ Point a,b; }; Line line[100]; int indl,indp; double Map[150][150]; bool dcmp(double x) { if(fabs(x)-0.0 <= eps) return true; return false; } void init(){ for(int i=1;i<=140; i++) { for(int j=1; j<=140; j++) Map[i][j] = 100000000.0; // Map[i][j] = (i!=j) ? 100000000.0 : 0; } } 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 cross(Point a,Point b,Point c) { if(dcmp((b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y))) return 0; return ((b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y)); } bool f(Point a,Point b,Point c,Point d) { if(min(a.x,b.x)<=max(c.x,d.x) && min(c.x,d.x)<=max(a.x,b.x) && min(a.y,b.y)<=max(c.y,d.y) && min(c.y,d.y)<=max(a.y,b.y)){ if(cross(a,b,c)*cross(a,b,d) < 0 && cross(c,d,a)*cross(c,d,b) < 0) return true; } return false; } bool pd(Point x,Point y){ for(int i=1; i<indl; i++) { if(f(x,y,line[i].a,line[i].b)) { return false; } } return true; } void jian(){ for(int i=1;i<=indp; i++) { if(i == 1 || i == indp) { for(int j=1; j<=indp; j++) { if(i!=j && pd(p[i],p[j])) { Map[i][j] = min(Map[i][j],dis(p[i],p[j])); } } continue; } for(int j=1; j<=indp; j++) { if(i != j && (((i-2)/4+1) != ((j-2)/4+1)) && pd(p[i],p[j])) { Map[i][j] = min(Map[i][j],dis(p[i],p[j])); } } } } int main(){ int n; while(scanf("%d",&n)!=EOF) { if(n == -1) break; init(); Point A,B; indl = 1,indp = 1; p[indp].x = 0, p[indp].y = 5; indp++; double a,b,c,d,e; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e); p[indp].x = a; p[indp++].y = b; p[indp].x = a; p[indp++].y = c; p[indp].x = a; p[indp++].y = d; p[indp].x = a; p[indp++].y = e; A.x = a; A.y = 0; B.x = a; B.y = b; line[indl].a = A; line[indl++].b = B; A.x = a; A.y = c; B.x = a; B.y = d; line[indl].a = A; line[indl++].b = B; A.x = a; A.y = e; B.x = a; B.y = 10; line[indl].a = A; line[indl++].b = B; } p[indp].x = 10; p[indp].y = 5; jian(); //建图 // 最短路 for(int k=1; k<=indp; k++) { for(int i=1; i<=indp; i++) { for(int j=1; j<=indp; j++) { if(Map[i][k]!=100000000.0 && Map[k][j] != 100000000.0) { Map[i][j] = min(Map[i][j],Map[i][k]+Map[k][j]); } } } } printf("%.2lf\n",Map[1][indp]); } return 0; }
相关文章推荐
- NYOJ 227 && poj1556 判断直线相交
- CF459E Pashmak and Graph && nyoj1208 水题系列 【贪心+dp】
- Nyoj 434&&Hud 1301 Jungle Roads[最小生成树Kruscal]
- nyoj&nbsp;587&nbsp;houseblocks
- 【矩阵快速幂】POJ 3233 && NYOJ 298 Matrix Power Series
- NYOJ3—多边形重心问题(计算几何)&&hdu1115 Lifting the Stone
- nyoj--86--找球号(一)(hash&&set&&二分)
- nyoj 疯牛 586 (二分&&枚举) 好题
- hdu1050&&nyoj220 Moving Tables(贪心)
- 蚂蚁的难题(二) && 首尾相连数组的最大子数组和(nyoj 745 && nyoj 983)
- NYOJ--364&&HDU--1052
- NYOJ 449 不再爱你…… (数学题&积分)
- NYOJ 608 508筹划工程&& HDU 1232 畅通工程
- NYOJ119 士兵杀敌(三)&&poj3264 Balanced Lineup(线段树)
- poj2299 Ultra-QuickSort&&NYOJ117 求逆序数 (树状数组求逆序对数+离散化)+(归并排序)
- 木棒 xynuoj 1737 && 心急的C小加 nyoj 236
- nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】
- HDU 1124 Factorial&&nyoj 84 阶乘的 0【数学】
- Hdu 2204 Eddy's爱好 && Nyoj 526 M^k数[容斥原理]
- POJ&&NYOJ--1328题Radar Installation