This line:

  tmin = (fabs(dot))/mod;

should read

  tmin = -dot/mod;

Change the rest of the program accordingly. E.g. fabs(dot) < ERR
should be -dot <= 0.

Also, you often test fabs(sth) < ERR, and this will work as long as
there are less than about 10 million flies because then it is the same
as testing fabs(sth) == 0. Consider the case of (10^8) - 1 flies at
position (0,0,0) standing still (speed = 0) and one fly at position
(1,0,0) with speed (-1,0,0). The minimal distance will be 0 at time 1
because then the one moving fly is at the origin, but because the
initial distance and the speed are 1/10^8 = 10^-8 which are smaller
than 10^7, you will set the minimum distance to 0 but you set the time
to 0 too, which is far from the correct answer of 1. You are testing
for scale, not for "zeroness", which means you probably put these
tests there for the wrong reason.

Peter

On Wed, Sep 16, 2009 at 8:00 PM, Seedrick <[email protected]> wrote:
>
> Someone please tell me what is the problem in my code
>
> #define sq(a) ((a)*(a))
> #define ERR 1e-7
> int main() {
>        freopen("B-small-attempt11.in","r",stdin);
>        freopen("B-small-attempt11.out","w",stdout);
>        int tc,i,j,k,n,a,b,c,va,vb,vc;
>        double u[3],ap,dot,dmin,tmin,mod;
>        cin >> tc;
>        for(i=0;i<tc;i++) {
>                cin >> n;
>                vector<double> p(3,0.0);
>                vector<double> v(3,0.0);
>                for(j=0;j<n;j++) {
>                        cin >> a >> b >> c >> va >> vb >> vc;
>                        p[0]+=a;
>                        p[1]+=b;
>                        p[2]+=c;
>                        v[0]+=va;
>                        v[1]+=vb;
>                        v[2]+=vc;
>                }
>                p[0]=p[0]/n;
>                p[1]=p[1]/n;
>                p[2]=p[2]/n;
>
>                v[0]=v[0]/n;
>                v[1]=v[1]/n;
>                v[2]=v[2]/n;
>
>                mod = sqrt ( sq(v[0])+sq(v[1])+sq(v[2]) );
>                if(mod < ERR )
>                {
>                        mod=0.0;
>                        dmin=sqrt (sq(p[0])+sq(p[1])+sq(p[2]));
>                        if(dmin < ERR)
>                                dmin =0.0 ;
>                        printf("Case #%d: %.8lf %.8lf\n",i+1,dmin,mod);
>                        continue;
>                }
>                u[0]=v[0]/mod;
>                u[1]=v[1]/mod;
>                u[2]=v[2]/mod;
>                ap=sq(p[0])+sq(p[1])+sq(p[2]);
>                dot=p[0]*u[0]+p[1]*u[1]+p[2]*u[2];
>                if(fabs(dot) < ERR)
>                {
>                                dmin = sqrt(ap);
>                                tmin=0.0;
>                }
>                else
>                {
>                        dmin= ap-sq(dot);
>                        dmin = sqrt(dmin);
>                        tmin = (fabs(dot))/mod;
>                }
>                printf("Case #%d: %.8lf %.8lf\n",i+1,dmin,tmin);
>        }
>        return 0;
> }
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to