Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我的算法不粉。。怎么老超时啊!!!

Posted by yzhw at 2009-01-23 16:32:50 on Problem 1034
# include <stdio.h>
# include <math.h>
typedef struct graph
{
   int x1;
   int y1;
   int x2;
   int y2;
   double len;
   int *link;
}graph;
typedef struct point_def
{
   int x;
   int y;
}point_def;
int pnum,vnum,*tres,*res,*hasvisit,maxnum=0,nownum=0;
point_def *t2;
graph *data;
int isp(point_def p1,point_def p2,point_def p3)
{
    if((double)(p2.y-p1.y)/(p2.x-p1.x)==(double)(p3.y-p2.y)/(p3.x-p2.x)) return 1;
    else return 0;
}
double cul(int x1,int y1,int x2,int y2)
{
 return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}

void deal(int num)
{
  if(num==pnum)
  {
   if(nownum>maxnum)
   {
    int i;
    for(i=1;i<pnum;i++) res[i]=tres[i];
   }
  }
  else if(nownum+pnum-num<maxnum) return;
  else
  {
      int i;
      tres[num]=0;
      deal(num+1);
      for(i=1;i<=data[num].link[0];i++)
       if(!hasvisit[data[num].link[i]]) 
       {
          hasvisit[data[num].link[i]]=1;
          nownum++;
          tres[num]=data[num].link[i];
          deal(num+1);
          hasvisit[data[num].link[i]]=0;
          nownum--;
       }
  }
}
          
int main()
{
/*------------------------------------------------------------------------*/
    int i,j;
    scanf("%d %d",&pnum,&vnum);
    point_def *t1=(point_def *)malloc(sizeof(struct point_def)*(pnum+1));
    t2=(point_def *)malloc(sizeof(struct point_def)*(vnum+1));
    data=(graph *)malloc(sizeof(struct graph)*(pnum+1));
    res=(int *)malloc(sizeof(int)*(pnum));
    tres=(int *)malloc(sizeof(int)*(pnum));
    hasvisit=(int *)malloc(sizeof(int)*(vnum+1));
    memset(res,0,sizeof(int)*(pnum));
    memset(tres,0,sizeof(int)*(pnum));
    memset(hasvisit,0,sizeof(int)*(vnum+1));
/*------------------------------------------------------------------------*/
    for(i=1;i<=pnum;i++)
       scanf("%d %d",&t1[i].x,&t1[i].y);
    for(i=1;i<pnum;i++)
    {
       data[i].x1=t1[i].x;
       data[i].y1=t1[i].y;
       data[i].x2=t1[i+1].x;
       data[i].y2=t1[i+1].y;
       data[i].len=cul(data[i].x1,data[i].y1,data[i].x2,data[i].y2);
    }
    for(i=1;i<=vnum;i++)  scanf("%d %d",&t2[i].x,&t2[i].y);
    
/*------------------构建图----------------------------*/
   for(i=1;i<pnum;i++)
   {
      int num=0,temp[101];
      for(j=1;j<=vnum;j++)
      {
       double temp1,temp2;
       if((temp1=cul(data[i].x1,data[i].y1,t2[j].x,t2[j].y))<=2*data[i].len)
         if((temp2=cul(data[i].x2,data[i].y2,t2[j].x,t2[j].y))<=2*data[i].len)
           if(temp1+temp2<=2*data[i].len) 
             temp[++num]=j;
       }
       data[i].link=(int *)malloc(sizeof(int)*(num+1));
       data[i].link[0]=num;
       for(j=1;j<=num;j++) data[i].link[j]=temp[j];
   }
/*-----------------------------------------------------*/
    deal(1);
/*-----------------------------------------------------*/ 
    point_def result[201];
    int result_p[201];
    int res_c=0,temp=0;
    for(i=1;i<pnum;i++)
    {
     result[++res_c]=t1[i];
     if(res[i]) result[++res_c]=t2[res[i]];
    }
    result[++res_c]=t1[pnum];
    for(i=1;i<=res_c;i++) result_p[i]=1;
    for(i=2;i<res_c;i++)
      if(isp(result[i-1],result[i],result[i+1]))
        {result_p[i]=0;temp++;}
    printf("%d\n",res_c-temp);
    for(i=1;i<res_c;i++)
      if(result_p[i])  printf("%d %d ",result[i].x,result[i].y);
    if(result_p[res_c]) printf("%d %d",result[res_c].x,result[res_c].y);
/*-----------------------------------------------------*/
    free(data);
    free(res);
    free(tres);
    free(hasvisit);
    free(t2);
    free(t1);
    system("pause");
    return 0;
}
    

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator