
This is a classic complete-search problem. With only 5,000 pairs of cows, only a maximum of 5000*5001/2 = 12,502,500 comparisons need to be made in the worst case.
Distance problems often sound like they need more math than they actually do. Finding the smallest or largest distance is generally (for normal cartesian distance) the same as finding the square of that same distance, thus relieving the program of the burden of calculating 12MM square roots.
A doubly nested loop surrounds the simple calculation and comparison:
#include <stdio.h>
double x[5000], y[5000];
#define SQ(z) ((z)*(z))
main () {
FILE *fin = fopen ("lonesome.in", "r");
FILE *fout = fopen ("lonesome.out", "w");
double farthest = 0.0;
int i1, i2; /* the answers */
int i, j, n;
fscanf (fin, "%d", &n);
for (i = 0; i < n; i++)
fscanf (fin, "%lf%lf", &x[i], &y[i]);
for (i = 0; i < n; i++) {
for (j = i+1; j < n; j++) {
double dist2 = SQ(x[j]-x[i]) + SQ(y[j]-y[i]);
if (dist2 > farthest) {
farthest = dist2;
i1 = i;
i2 = j;
}
}
}
fprintf (fout, "%d %d\n", i1+1, i2+1);
exit (0);
}