关闭

zju 1721 The Doors

755人阅读 评论(0) 收藏 举报

I theat this problem as a shortest path problem, And i do it as the follow steps:
1). check whether point i and point j can direct arrived or not, if can and sign the distance between i and j,else sign the distance as infinte
2). use DJ algorithm to get the shortest distance between [0,5] to [10,5]

but , i got TLE

Later,I got AC Very Happy
this problem i should AC in one time
in original, i set max array size M = 50, i think 9 * 4 + 2 = 38 < 50
now, i change M = 100. ==> AC
I feel crazy!! Evil or Very Mad

My algorithm is all right!
AC==>0.00S,480K
by the way, i got the max n = 18, Very Happy (test data may give in repeatition)
18 * 4 + 2 = 74 < 100
SO, ....

////////////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <string.h>
#include <math.h>

const double INF = 1E15 ;

const int M = 74 ;
struct POINT {
 double x, y ;
} pt[M] ;

struct WALL {
 double x, y1, y2 ;
} wall [M] ;

bool flag[M] ;
double value[M][M], dist[M] ;

double GetDist ( int i, int j )
{
 return sqrt ( (pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)+(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y)) ;
}
int main()
{
 int n ;
 while ( scanf ( "%d", &n ) && n != -1 )
 {
  memset ( flag, 0, sizeof(flag) ) ;
  pt[0].y = 5 ;

  double x, y1, y2, y3, y4 ;
  int i, j, k, nPtNum = 1, nWallNum = 0 ;
  for ( i = 0; i < n; i++ )
  {
   scanf ( "%lf %lf %lf %lf %lf", &x, &y1, &y2, &y3, &y4 ) ;
   
   pt[nPtNum].x = x ;
   pt[nPtNum++].y = y1 ;
   pt[nPtNum].x = x ;
   pt[nPtNum++].y = y2 ;
   pt[nPtNum].x = x ;
   pt[nPtNum++].y = y3 ;
   pt[nPtNum].x = x ;
   pt[nPtNum++].y = y4 ;

   wall[nWallNum].x = x ;
   wall[nWallNum].y1 = 0 ;
   wall[nWallNum++].y2 = y1 ;
   wall[nWallNum].x = x ;
   wall[nWallNum].y1 = y2 ;
   wall[nWallNum++].y2 = y3 ;
   wall[nWallNum].x = x ;
   wall[nWallNum].y1 = y4 ;
   wall[nWallNum++].y2 = 10 ;
  }
  pt[nPtNum].x = 10 ;
  pt[nPtNum++].y = 5 ;

  int count = 0 ;
  for ( i = 0; i < nPtNum; i++ )
  {
   for ( j = i+1; j < nPtNum; j++ )
   {
    int beg = ceil(double (i)/4), end = ceil(double (j)/4) ;
    bool isValid = true ;
    if ( beg == end )
     isValid = false ;
    for ( k = beg + 1; k < end; k++ )
    {
     int cur = ( k - 1 ) * 3 ;
     double y0 = pt[i].y - ( wall[cur].x - pt[i].x ) * ( pt[i].y - pt[j].y ) / /
        ( pt[j].x - pt[i].x ) ;
     count++ ;
     if ( y0 > wall[cur].y1 && y0 < wall[cur].y2  )
     {  
      isValid = false ;
      break ;
     }
     count++ ;
     if ( y0 > wall[cur+1].y1 && y0 < wall[cur+1].y2 )
     {
      isValid = false ;
      break ;
     }
     count++ ;
     if ( y0 > wall[cur+2].y1 && y0 < wall[cur+2].y2 )
     {
      isValid = false ;
      break ;
     } 
    }
    if ( isValid )
     value[i][j] = value[j][i] = GetDist ( i, j ) ;
    else
     value[i][j] = value[j][i] = INF ;
   }
   if ( value[0][nPtNum-1] != INF )
    break ;
  }
  if ( i == 0 )
  {
   printf ( "%.2lf/n", value[0][nPtNum-1] ) ;
   continue ;
  }

  int cur = 0, end = nPtNum - 1 ;
  for ( i = 1; i < nPtNum; i++ )
   dist[i] = value[0][i] ;
  flag[0] = true ;
  while ( cur != end )
  {
   double min = INF ;
   for ( i = 1; i < nPtNum; i++ )
   {
    if ( !flag[i] && dist[i] < min )
    {
     min = dist[i] ;
     cur = i ;
    }
   }
   flag[cur] = true ;

   for ( i = 1; i < nPtNum; i++ )
   {
    if ( !flag[i] && min + value[cur][i] < dist[i] )
     dist[i] = min + value[cur][i] ;
   }   
  }
  printf ( "%.2lf/n", dist[end] ) ;
 }
 return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:79854次
    • 积分:1019
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:24篇
    • 译文:0篇
    • 评论:2条
    最新评论
    牛博
    影视资源