您的位置:首页 > 其它

POJ-2349 Arctic Network (最小生成树)

2017-12-26 23:28 267 查看
Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23716 Accepted: 7274
DescriptionThe Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts willin addition have a satellite channel. 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.InputThe first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost inkm (coordinates are integers between 0 and 10,000).OutputFor each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.Sample Input
1
2 4
0 100
0 300
0 600
150 750
Sample Output
212.13
#include <cstdio>#include <cmath>#include <algorithm>using namespace std;struct edge{int from, to, val;bool operator < (const edge& x) const{return val < x.val;}}e[320005];int p[800], x[800], y[800];double dis[320004];int findset(int x){return p[x] == x ? x : p[x] = findset(p[x]);}int main(){int T;scanf("%d", &T);while(T--){int s, u, v, n;scanf("%d %d", &s, &n);for(int i = 1; i <= n; ++i){scanf("%d %d", &x[i], &y[i]);}int num = 0;for(int i = 1; i <= n; ++i){for(int j = i + 1; j <= n; ++j){e[++num].from = i;e[num].to = j;e[num].val = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);}}for(int i = 1; i <= n; ++i){p[i] = i;}sort(e + 1, e + 1 + num);int tot = 0, ans = 0;for(int i = 1; i <= num; ++i){u = findset(e[i].from);v = findset(e[i].to);if(u != v){p[u] = v;++tot;dis[tot] = sqrt(e[i].val);if(tot == n - 1){break;}}}reverse(dis + 1, dis + 1 + tot);printf("%.2f\n", dis[s]);}}/*题意:100个点,求最小生成树,s个卫星,卫星可以代替一条边。思路:最小生成树,改变一下,把最小生成树的所有边存下,用卫星代替长的边即可。*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息