tags:

views:

39

answers:

2

why my program Time Limited Error? because of the sort? this is the question link text

#include <cstdio>
#include <cstring>

using namespace std;

int map[22][44];
int book[22];
int total;
int sum;
int way[22];
int tails[22]; 
int tail; 

void init()
{
 memset(map,0,sizeof(map));
 memset(book,0,sizeof(book));
 sum =0;
 memset(way,0,sizeof(way));
 way[1]=1;
 memset(tails,0,sizeof(tails));
}

void sort()
{
 int t;
 for (int i=1;i<=22;i++)
  {
   if (tails[i]==0)
    break;
   else
    {
     for (int j=1;j<=tails[i]-1;j++)
      for (int k=j+1;k<=tails[i];k++)
       {
        if (map[i][j] > map[i][k])
         {
          t = map[i][j];
          map[i][j]=map[i][k];
          map[i][k]=t;
         }
       } 
    }
  }
}

void dfs(int x,int y)
{
 if ((x < 1)||(x > 22))
  return;
 if (book[x]==1)
  return;
 //printf("%d \n",x);
 if (x == total)
  {
   sum++;
   for (int i=1;i<=y-1;i++)
   {
    printf("%d ",way[i]);
   }
   printf("%d",total);
   printf("\n");
   return;
  }
 tail = tails[x];
 for (int i=1;i<=43;i++)
  {
   book[x]=1;
   way[y]=x;
   dfs(map[x][i],y+1);
   book[x]=0;
  } 
}

int main()
{
 int temp1,temp2;
 //freopen("ex.in","r",stdin);
 //freopen("ex.out","w",stdout);
 int c = 0;
 while(scanf("%d",&total)!=EOF)
 {
 c++;
 printf("CASE ");
 printf("%d",c);
 printf(":");
 printf("\n");
 init();
 for (;;)
  {
   scanf("%d%d",&temp1,&temp2);
   if ((temp1 == 0)&&(temp2 == 0))
    break;
   else
    {
     tails[temp1]++;
     tail = tails[temp1];
     map[temp1][tail]=temp2;
     tails[temp2]++;
     tail = tails[temp2];
     map[temp2][tail]=temp1; 
    }
  }
  sort();
  dfs(1,1);
  printf("There are ");printf("%d",sum);printf(" routes from the firestation to streetcorner ");printf("%d",total);printf(".");
  printf("\n");
 }
 return 0;
}
A: 

For a start, you're accessing tails and map past the end. C++ arrays are zero-indexed, so the first element is 0, and the last valid elements are tails[21] and map[21][43].

Matthew Flaschen
of course i konw it...
CherryUnix
+2  A: 

Because your sorting algoritm is in worst-case O(n*n), you can use InnoSort for better worst-case complexity O(n*log(n)).

You are using C++ then use sort function from <algorithm> header to do this simplest.

Documentation you can find at http://www.sgi.com/tech/stl/sort.html

Svisstack
so should i use heapsort or q-sort?because i don't like use stl in ACM....
CherryUnix
@CherryUnix: quicksort have worstcase complexity O(n*n) then is not better than quicksort and heapsort too. Then get rewrite this algorithm from STL headers. STL is a part of C++ project and you can use it where you want.
Svisstack
en....in fact we are not allowed to use stl in NOI(National Olympiad in Informatics)except map..
CherryUnix
In general STL is not allowed in such competitions because it gives you an unfair advantage over the C and Java crowd. But if you have `std::map` (and only that) you can still benefit. It has a constructor that will construct a sorted container from an iterator range. Sure, you'd have to throw in a dummy value (as you can't have `std::set`), but you would simply ignore it.
MSalters
thanks a lot my program had been Accepted
CherryUnix