POJ2349--Arctic Network
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10955 | Accepted: 3592 |
Description
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.
Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.
Input
Output
Sample Input
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
Source
方法1:二分D+并查集,如果连通块数目小于等于s,则D可能可以更小,不然更大,poj上耗时大概800多ms
/************************************************************************* > File Name: POJ2349.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年01月25日 星期日 13时33分36秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int father[555]; struct node { int x, y; }point[555]; int find (int x) { if (father[x] == -1) { return x; } return father[x] = find (father[x]); } double dist(node a, node b) { return sqrt ((double)((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))); } int main () { int t; scanf("%d", &t); while (t--) { int s, n; scanf("%d%d", &s, &n); double l = 0; double r = 20000; double mid; for (int i = 1; i <= n; ++i) { scanf("%d%d", &point[i].x, &point[i].y); } double ret; while (l + 1e-6 < r) { int ans = n; memset (father, -1, sizeof(father)); mid = (l + r) / 2; for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { if (dist(point[i], point[j]) + 1e-6 <= mid) { int u = find (i); int v = find (j); if (u != v) { father[u] = v; --ans; } } } } if (ans <= s) { r = mid; ret = mid; } else { l = mid; } } printf("%.2f\n", ret); } return 0; }
法二:MST,标记出加入到MST中的边,去掉最大的s-1条,剩下的边里最大的就是D,耗时大概几十ms
/************************************************************************* > File Name: poj2349.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年01月25日 星期日 13时08分14秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; double mat[555][555]; double lowc[555]; int pre[555]; bool vis[555]; double dist[555]; int n, s; struct node { int x, y; }point[555]; void prim (int n, int s) { memset (vis, 0, sizeof(vis)); for (int i = 2; i <= n; ++i) { lowc[i] = mat[1][i]; pre[i] = 1; } pre[1] = -1; vis[1] = 1; lowc[1] = 0; int cnt = 0; for (int i = 2; i <= n; ++i) { double minc = 0x3f3f3f3f; int p; for (int j = 1; j <= n; ++j) { if (!vis[j] && minc > lowc[j]) { minc = lowc[j]; p = j; } } dist[cnt++] = minc; vis[p] = 1; for (int j = 1; j <= n; ++j) { if (!vis[j] && mat[p][j] < lowc[j]) { lowc[j] = mat[p][j]; pre[j] = p; } } } sort (dist, dist + cnt); printf("%.2f\n", dist[cnt - s]); } double Dist (node a, node b) { return sqrt (double ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))); } int main () { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &s, &n); for (int i = 1; i <= n; ++i) { scanf("%d%d", &point[i].x, &point[i].y); } memset (mat, 0x3f3f3f3f, sizeof(mat)); for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { mat[i][j] = mat[j][i] = Dist(point[i], point[j]); } } prim(n, s); } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。