poj 1556 (Dijkstra + Geometry 线段相交)
生活随笔
收集整理的這篇文章主要介紹了
poj 1556 (Dijkstra + Geometry 线段相交)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接:http://poj.org/problem?id=1556
The Doors| Time Limit:?1000MS | ? | Memory Limit:?10000K |
| Total Submissions:?6216 | ? | Accepted:?2495 |
Description
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.?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
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
1 5 4 6 7 8 2 4 2 7 8 9 7 3 4.5 6 7 -1Sample Output
10.00 10.06這題處理起來挺難的,要把輸入的點存到圖里,用Dijkstra求出最短路徑,存圖的過程是,判斷任意兩點連成的線,橫坐標不能相同,并且如果,橫坐標與線上的橫坐標不相同,就要判斷是否相交,相交則行不通
否則存圖,用Dijkstra搜出最短的路徑即可
還有,要注意細節
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <math.h> 6 #include <algorithm> 7 8 #define eps 1e-6 9 #define INF 1000000000 10 typedef struct point 11 { 12 double x,y; 13 }point; 14 15 typedef struct beline 16 { 17 point st,ed; 18 }beline; 19 20 using namespace std; 21 22 point p[1005]; 23 double mp[1005][1005]; 24 double d[1005]; 25 int visit[1005]; 26 27 bool dy(double x,double y){ return x > y+eps; } 28 bool xy(double x,double y){ return x < y-eps; } 29 bool dyd(double x,double y){ return x > y-eps; } 30 bool xyd(double x,double y){ return x < y+eps; } 31 bool dd(double x,double y){ return fabs(x - y)<eps; } 32 33 double crossProduct(point a,point b,point c) 34 { 35 return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x); 36 } 37 double Dist(point a,point b) 38 { 39 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 40 } 41 42 bool onSegment(point a,point b,point c) 43 { 44 double maxx=max(a.x,b.x); 45 double maxy=max(a.y,b.y); 46 double minx=min(a.x,b.x); 47 double miny=min(a.y,b.y); 48 if(dd(crossProduct(a,b,c),0.0)&&dy(c.x,minx)&&xy(c.x,maxx) 49 &&dy(c.y,miny)&&xy(c.y,maxy)) 50 return true; 51 return false; 52 } 53 54 bool segIntersect(point p1,point p2,point p3,point p4) 55 { 56 double d1 = crossProduct(p3,p4,p1); 57 double d2 = crossProduct(p3,p4,p2); 58 double d3 = crossProduct(p1,p2,p3); 59 double d4 = crossProduct(p1,p2,p4); 60 if(xy(d1*d2,0.0)&&xy(d3*d4,0.0)) 61 return true; 62 if(dd(d1,0.0)&&onSegment(p3,p4,p1)) 63 return true; 64 if(dd(d2,0.0)&&onSegment(p3,p4,p2)) 65 return true; 66 if(dd(d3,0.0)&&onSegment(p1,p2,p3)) 67 return true; 68 if(dd(d4,0.0)&&onSegment(p1,p2,p4)) 69 return true; 70 return false; 71 } 72 73 void Dijkstra(int n) 74 { 75 int i,y; 76 memset(visit,0,sizeof(visit)); 77 for(i=0; i<n; i++) 78 d[i] = mp[0][i]; 79 d[0] = 0; 80 for(i=0; i<n; i++) 81 { 82 int m=INF,x; 83 { 84 for(y=0; y<n; y++) 85 { 86 if(!visit[y] && d[y]<=m) 87 { 88 m = d[ x = y ]; 89 } 90 } 91 visit[x]=1; 92 for(y=0; y<n; y++) 93 { 94 if(!visit[y] && d[y] > d[x]+mp[x][y]) 95 { 96 d[y] = d[x] + mp[x][y]; 97 } 98 } 99 } 100 } 101 } 102 103 int main() 104 { 105 int n,m,i,j,k,t; 106 double a,b,c,d1,e; 107 beline li[10005]; 108 beline tmp; 109 p[0].x=0;p[0].y=5;//freopen("in.txt","r",stdin); 110 while(scanf("%d",&n)!=EOF && n!=-1) 111 { 112 for(i=0; i<1005; i++) 113 for(j=0; j<1005; j++) 114 mp[i][j] = INF; 115 int cas=1,css=0; 116 for(i=0; i<n; i++) 117 { 118 scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d1,&e); 119 li[css].st.x=a; 120 li[css].st.y=0; 121 p[cas].x=a; li[css].ed.x=a; 122 p[cas++].y=b;li[css++].ed.y=b; 123 p[cas].x=a; li[css].st.x=a; 124 p[cas++].y=c;li[css].st.y=c; 125 p[cas].x=a; li[css].ed.x=a; 126 p[cas++].y=d1;li[css++].ed.y=d1; 127 p[cas].x=a; li[css].st.x=a; 128 p[cas++].y=e;li[css].st.y=e; 129 li[css].ed.x=a; 130 li[css++].ed.y=10; 131 } 132 p[cas].x=10.0;p[cas].y=5.0; 133 for(i=0; i<=cas; i++) 134 { 135 for(j=i+1; j<=cas; j++) 136 { 137 int ok=0; 138 for(k=0; k<css; k++) 139 { 140 if(dd(p[i].x,p[j].x)||!dd(p[i].x,li[k].st.x)&&!dd(p[j].x,li[k].st.x)&&(segIntersect(p[i],p[j],li[k].st,li[k].ed))) 141 { 142 ok=1; 143 break; 144 } 145 } 146 if(!ok) 147 { 148 mp[j][i] = mp[i][j] = Dist(p[i],p[j]);//printf("%d %d %lf ^^\n",i,j,mp[i][j]); 149 } 150 } 151 } 152 Dijkstra(cas+1); 153 printf("%.2lf\n",d[cas]); 154 } 155 return 0; 156 } View Code
?
轉載于:https://www.cnblogs.com/ccccnzb/p/3893820.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的poj 1556 (Dijkstra + Geometry 线段相交)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS经常使用加密方式(MD5,AES,
- 下一篇: as3 代码加解密