Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19510 Accepted Submission(s): 6116
2 2 10 10 20 20 3 1 1 2 2 1000 1000
1414.2 oh!
第一次是RE,原因是edge开小了。这道题是双重for循环,因此开的值至少要大于c*(c-1)。
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int per[110], c, k; double x[110], y[110]; struct node{ int u, v; double s; }; node edge[11000]; double cmp(node a, node b){ return a.s < b.s; } void init(int c){ int i; for(i = 0; i <= c; ++i){ per[i] = i; } } int find(int x){ int r = x; while(per[r] != r) r = per[r]; per[x] = r; return r; } void kruskal(){ init(c); sort(edge, edge + k, cmp); int fx, fy, ans = 1, i; double sum = 0.0; for(i = 0; i < k; ++i){ fx = find(edge[i].u); fy = find(edge[i].v); if(fx != fy){ sum += edge[i].s * 100; per[fx] = fy; ans++; } } if(ans == c) printf("%.1lf\n",sum); else printf("oh!\n"); } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&c); memset(x, 0, sizeof(x)); memset(y, 0, sizeof(y)); int i; for(i = 0; i < c; i++){ scanf("%lf%lf",&x[i], &y[i]); } int j; k = 0; for(i = 0; i < c - 1; ++i){ for(j = i + 1; j < c; ++j){ double l = sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2)); if(l >= 10.0 && l <= 1000.0){ edge[k].s = l; edge[k].u = i; edge[k].v = j; k++; } } } kruskal(); } return 0; }