Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
新手用Prim飘过。。。#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> using namespace std; #define INF 0x7fffffff #define maxn 550 struct node{ int x; int y; }point[maxn]; double map[maxn][maxn]; double dis[maxn]; bool used[maxn]; double bian[maxn]; int s, p, h; double len(int x1, int x2, int y1, int y2) { return sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)); } void Prim() { int i, j, k; h=0; memset(used, 0, sizeof(used)); used[1] = 1; for(i=1; i<=p; i++) { dis[i] = map[1][i]; } for(i=1; i<=p; i++) { double min = INF; for(j=1; j<=p; j++) { if(!used[j] && dis[j]<min) { k = j; min = dis[j]; } } used[k] = 1; bian[h++]=min; for(j=1; j<=p; j++) { if(!used[j] && dis[j]>map[k][j]) { dis[j] = map[k][j]; } } } } int main() { int t, i, j; scanf("%d",&t); while(t--) { scanf("%d%d",&s, &p); for(i=1; i<=p; i++) { scanf("%d%d", &point[i].x, &point[i].y); } for(i=1; i<=p; i++) { for(j=1; j<=p; j++) { if(i == j) map[i][j] = 0; else map[i][j] = INF; } } for(i=1; i<=p; i++) { for(j=1; j<=p; j++) { map[i][j] = len(point[i].x, point[j].x, point[i].y, point[j].y); } } Prim(); sort(bian, bian+h); printf("%0.2f\n",bian[h-2-s+1]); // for(i=0; i<h-1; i++) // printf(" %.2lf", bian[i]); // printf("\n"); } return 0; } /*不知道为什么用c++交显示语法错误,然后用G++交%.2lf就WA。。。。 说好的一遍过呢。。。5555555 T_T*/ Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator