Skip to content

Instantly share code, notes, and snippets.

@linkkingjay
Last active August 29, 2015 14:18
Show Gist options
  • Save linkkingjay/0934659b3bff4ae6105f to your computer and use it in GitHub Desktop.
Save linkkingjay/0934659b3bff4ae6105f to your computer and use it in GitHub Desktop.
#include <stdio.h>
int x[100001];
int y[100001];
int flag[100001] = {0};
int min(int a, int b) {
return a > b ? b : a;
};
int abs(int x) {
return x > 0 ? x : -x;
};
int main() {
int n;
int i;
int c = 0;
int next = 0;
int min_steps, this_steps;
long long sum = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d", &x[i], &y[i]);
}
while (c < n) {
c++;
flag[next] = 1;
min_steps = 1000000001;
for (i = 0; i < n; i++) {
if (flag[i] == 0) {
this_steps = min(abs(x[next] - x[i]), abs(y[next] - y[i]));
if (this_steps < min_steps) {
min_steps = this_steps;
next = i;
}
}
}
if (min_steps == 1000000001) {
min_steps = 0;
}
sum += min_steps;
}
printf("%lld\n", sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment