1. 程式人生 > >[POJ2349] Arctic Network [最小生成樹]

[POJ2349] Arctic Network [最小生成樹]

好像是個稠密圖 總之跑跑prim就過了 注意要求的是第K+1長邊

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstring>
#include<queue>
using namespace std;
queue<int>Q;
int S, P;
double
dist[505]; double x[505]; double y[505]; int lstot; double dis[505][505]; double lst[505]; bool vis[505]; inline double calc(const double &deltax, const double &deltay) { return sqrt(deltax * deltax + deltay * deltay); } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d", &
S, &P); for (int i = 1; i <= P; ++i) { scanf("%lf%lf", &x[i], &y[i]); } for (int i = 1; i <= P; ++i) { for (int j = 1; j <= P; ++j) { dis[i][j] = calc(x[i]-x[j],y[i]-y[j]); } } memset(vis, 0, sizeof(vis)); Q.push(1); lstot = 0; vis[1] = 1; for (int i =
1; i <= P; ++i) { dist[i] = dis[1][i]; } for (int i = 1; i < P; ++i) { double mn = 1145143810.0; int mnInd = 0; for (int j = 1; j <= P; ++j) { if (vis[j]) continue; if (dist[j] < mn) { mn = dist[j]; mnInd = j; } } vis[mnInd] = 1; for (int j = 1; j <= P; ++j) { if (vis[j]) continue; dist[j] = min(dist[j], dis[mnInd][j]); } lst[++lstot] = mn; } sort(lst+1, lst+1+lstot); printf("%.2f\n", lst[P-S]); } return 0; }