by zsepi » Wed Jan 15, 2003 6:00 am
honestly, if someone could explain me why on earth I get runtime error with this problem, would appreciate it... thanx
[c]#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MX 101
typedef struct _edge {
int v1,v2;
double cost;
}edge;
edge e[5001];
double x[MX],y[MX];
int compare(const void *a, const void *b) {
double x;
edge *i,*j;
i=(edge *)a;
j=(edge *)b;
x=i->cost-j->cost;
if(x==0.0) return 0;
else if(x>0) return 1;
else return -1;
}
void getData(int n) {
int i,j,k;
double dx,dy;
for(i=0;i<n;i++) scanf("%lf %lf",&x[i],&y[i]);
for(i=0,k=0;i<n;i++) {
for(j=i+1;j<n;j++) {
dx=x[i]-x[j];
dy=y[i]-y[j];
e[k].v1=i;
e[k].v2=j;
e[k].cost=sqrt(dx*dx+dy*dy);
k++;
}
}
qsort(e,k,sizeof(edge),compare);
}
void kruskal(int n) {
char status[n];
int i;
double cost=0.0;
for(i=0;i<n;i++) status[i]='0';
n--;
i=0;
cost=0;
while(n>0) {
if(status[e[i].v1]=='0' || status[e[i].v2]=='0') {
cost+=e[i].cost;
status[e[i].v1]='1';
status[e[i].v2]='1';
n--;
}
i++;
}
printf("%.2lf\n",cost);
}
main() {
int N,n;
scanf("%d",&N);
while(N-->0) {
scanf("%d",&n);
getData(n);
kruskal(n);
if(N!=0) printf("\n");
}
}[/c]
Dealing with failure is easy: Work hard to improve.
Success is also easy to handle: You've solved the wrong problem. Work hard to improve.