[POJ 2349] Arctic Network【Kruskal】
Problem:
Time Limit: 2000MS | Memory Limit: 65536K |
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
Solution:
这一题还是很水。。
本来想用 Prim 实现最小生成树,但是发现这道题用 Kruskal 处理更方便,在只剩 S 个连通块时可以直接中断输出答案即可。
Code: O(TP2logP2) [2140K, 172MS]
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int T, S, P; int x[502], y[502]; struct Edge{ int u, v; double dis; inline bool operator < (const Edge &edge2) const {return dis < edge2.dis;} } E[125002]; int tope, fa[502]; #define sqr(x) ((x) * (x)) inline double Euclid(int u, int v) {return sqrt((double)(sqr(x[u] - x[v]) + sqr(y[u] - y[v])));} inline void ds_init() {for(register int i = 1; i <= P; i++) fa[i] = i;} inline int ds_find(const int &u){ if(fa[u] == u) return u; return fa[u] = ds_find(fa[u]); } inline bool ds_union(const int &u, const int &v){ int ancu = ds_find(u), ancv = ds_find(v); if(ancu == ancv) return 0; fa[ancu] = ancv; return 1; } int main(){ scanf("%d", &T); while(T--){ scanf("%d%d", &S, &P); for(register int i = 1; i <= P; i++) scanf("%d%d", x + i, y + i); tope = 0; for(register int i = 1; i <= P; i++) for(register int j = i + 1; j <= P; j++) E[++tope].u = i, E[tope].v = j, E[tope].dis = Euclid(i, j); sort(E + 1, E + tope + 1); ds_init(); int blockcnt = P; for(register int i = 1; i <= tope; i++) if(ds_union(E[i].u, E[i].v)) if(--blockcnt == S){ printf("%.2f\n", E[i].dis); break; } } return 0; }
发表评论