The absolute value of each coordinate is less than or equal to 1000. So distance between two points may be greater than your m (= 1009) in min1(). This may return garbage value of index;
???
http://www.codechef.com/viewsolution/3324657 changing initialisation of key[i] and m = 10^7 gives AC.