poj1556

题目大意:在10*10的空间里,中间有几堵平行于竖直方向的墙,每个墙由两扇门,求从(0,5)-->(10,5)

的最短路径

思路:把门边沿的点两点建立一条边(中间无墙挡住),和起点,终点同样建边。。最后就成最短路。。

         网上大部分Dijkstra,其实数据很小,floyed足以搞定。。

 PS:这道题数据很弱,但是弱菜下标写错了。。调了非常久。。。还有看到网上有的写了接近300行代码吓尿了。。

写的很搓的代码:

  1 /*
  2   Time:2013-03-25 23:56:19
  3   State: Accepted 
  4 */
  5 
  6 #include<iostream>
  7 #include<cstring>
  8 #include<string>
  9 #include<cstdlib>
 10 #include<cstdio>
 11 #include<fstream>
 12 #include<cmath>
 13 #include<algorithm>
 14 #define INF 1000000
 15 #define ee 1e-8
 16 using namespace std;
 17 int n, tot; 
 18 double a[200][6] , map[202][202] ;
 19 int pos[200][6],  p1,  p2 , st , en;
 20 
 21 
 22 void init(){
 23      memset(a, 0, sizeof(a));
 24      tot = 0;
 25      for (int i = 0;  i<= 200; ++i)
 26          for (int j = 0; j <= 200; ++j)
 27             map[i][j] = INF;
 28      for (int i = 1;  i <= n; ++i){
 29          scanf("%lf%lf%lf%lf%lf",&a[i][0], &a[i][1] , 
 30                      &a[i][2] , &a[i][3], &a[i][4] );
 31          for (int j = 1; j <= 4; ++j) pos[i][j] = i + j * n - n;
 32      }
 33 }
 34 
 35 double work_x(double x2, double y2, double x1 , double y1){
 36       return x1*y2 - x2*y1;
 37 }
 38 
 39 void work(double x1 , double y1, double x2 , double y2){
 40      double x , y3 , y4;
 41      bool flag;
 42      for (int i = st; i <= en ; ++i){
 43          flag = 0;
 44          x = a[i][0]; 
 45          y3 = a[i][1];
 46          y4 = a[i][2];
 47          if (work_x(x1 - x , y1 - y3,  0, y4-y3)*work_x(x2 -x , y2 - y3, 0, y4-y3) < 0
 48             && work_x(x-x1 , y3-y1, x2 - x1, y2-y1)*work_x(x-x1 , y4-y1, x2-x1, y2-y1) < 0) flag = 1;
 49          y3 = a[i][3];
 50          y4 = a[i][4];     
 51          if (work_x(x1 -x , y1 - y3,  0, y4-y3)*work_x(x2 -x , y2 - y3, 0, y4-y3) < 0
 52             && work_x(x-x1 , y3-y1, x2 - x1, y2-y1)*work_x(x-x1 , y4-y1, x2-x1, y2-y1) < 0) flag = 1;
 53          if (!flag) return;
 54       
 55      }
 56     map[p2][p1] = map[p1][p2] = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
 57 }
 58 
 59 void solve(){
 60      tot = n * 4 + 1;
 61      for (int i = 1; i <= n; ++i)
 62       for (int k = 1; k <= 4; ++k){
 63       /*  for (int k1 = k + 1; k1 <= 4; ++k1)
 64                   map[pos[i][k1]][pos[i][k]] =
 65                   map[pos[i][k]][pos[i][k1]] = a[i][k1] - a[i][k]; */ 
 66         p1 = pos[i][k];
 67         p2 = tot;
 68         en = n;
 69         st = i + 1;
 70         work(a[i][0],a[i][k],10,5);
 71         p1 = 0;
 72         p2 = pos[i][k];
 73         en = i - 1;
 74         st = 1;
 75         work(0,5,a[i][0],a[i][k]);
 76         for (int j = 1; j < i; ++j)
 77           for (int l = 1; l <= 4 ; ++l){
 78                st = j + 1;
 79                p1 = pos[j][l];
 80                work(a[j][0], a[j][l], a[i][0], a[i][k]);
 81           }
 82         
 83       }   
 84       p1 =0;
 85       p2 = tot;
 86       en = n;
 87       st = 1;
 88       work(0,5,10,5);
 89     /*  for (int i = 0; i <= tot; ++i)
 90         for (int j =i + 1; j <=tot; ++j)
 91           if (map[i][j]!=INF) printf("map[%d][%d] = %lf\n",i , j ,map[i][j]);*/
 92       for (int k = 0; k <= tot; ++k)
 93         for (int i = 0; i <= tot; ++i)
 94            for (int j = 0; j <= tot; ++j){
 95                 if (i == j || j == k || k == i) continue;
 96                 map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
 97            }
 98       printf("%0.2f\n",map[0][tot]);
 99      
100              
101 }
102 
103 int main(){
104     freopen("poj1556.in","r",stdin);
105     freopen("poj1556.out","w",stdout);
106     while (scanf("%d", &n) != EOF && n != -1){
107          init();
108          solve();    
109     }
110     fclose(stdin); fclose(stdout);   
111 }

 

posted on 2013-03-26 00:21  yzcstc  阅读(176)  评论(0编辑  收藏  举报