題意:算法
n個無線AP,有xy座標屬性,如今n個無線AP要橋接在一塊兒不能斷開鏈接,如今要求無線AP無線網絡的覆蓋半徑最小是多少網絡
分析:spa
看起來是像是最小生成樹,這裏是是求生成樹中最長的邊最短,就是最小瓶頸生成樹。code
能夠證實最小瓶頸生成樹就是最小生成樹,詳細看劉汝佳《算法入門經典訓練指南》343頁。blog
當時現場的時候,想試試最小生成樹了,結果覺得n方複雜度過不去,就沒寫,如今想起來,真是坑哦。string
這題n有10000,因此不能直接建鄰接矩陣,而是採用動態計算距離就好了。it
比賽結束了隨便一寫就A了。。。io
代碼:入門
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 6 using namespace std; 7 8 const int inf = 0x3f3f3f3f; 9 const int maxn = 10010; 10 11 double x[maxn]; 12 double y[maxn]; 13 14 int n; 15 16 double lowc[maxn]; 17 bool vis[maxn]; 18 19 double dis(double x1, double y1, double x2, double y2) { 20 return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)); 21 } 22 23 double prim() { 24 double res = 0; 25 memset(vis, 0, sizeof(vis)); 26 vis[0] = true; 27 for(int i = 1; i < n; i++)lowc[i] = dis(x[0], y[0], x[i], y[i]); 28 for(int i = 1; i < n; i++) { 29 double minc = inf; 30 int p = -1; 31 for(int j = 0; j < n; j++) { 32 if(!vis[j] && lowc[j] < minc) { 33 minc = lowc[j]; 34 p = j; 35 } 36 } 37 res = max(res, minc); 38 vis[p] = true; 39 for(int j = 0; j < n; j++) { 40 double d = dis(x[p], y[p], x[j], y[j]); 41 if(!vis[j] && lowc[j] > d) { 42 lowc[j] = d; 43 } 44 } 45 } 46 return res / 2; 47 } 48 49 50 int main() { 51 while(~scanf("%d", &n)) { 52 for(int i = 0; i < n; i++) { 53 scanf("%lf%lf", &x[i], &y[i]); 54 } 55 printf("%.9f\n", prim()); 56 57 } 58 59 return 0; 60 61 }