標籤:tran lib 等於 iter imu 最小 between can channel
2017-08-04 16:19:13
writer:pprp
題意如下:
Description
The 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 will in 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.
Input
The 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 in km (coordinates are integers between 0 and 10,000).
Output
For 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
12 40 1000 3000 600150 750
Sample Output
212.13
題意簡化:
個人覺得這道題最難的在於理解,簡化以後的題意是:
? n個網站,s個衛星系統,每個衛星系統只能安排在一個網站
? 有衛星系統的網站間通訊不需要代價
? 任意兩點(i, j)間皆可通訊,代價為dis[i][j]
? 請用最小的代價使得任意兩個網站間均可以通訊
? n, s <= 1000
分析:
n個網站,應該用Kruskal演算法進行最小產生樹的進行;用ans數組記錄下來從小到大的邊的邊權,
最終結果就應該是ans[num - s] num是最小產生樹的邊的數目,等於是最後n個比較大的被捨去,需要最大的就是被捨去以後最大的點
代碼及分析:
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;//n個衛星,m個基站int n, m;int x, y;const int maxn = 505;const int INF = 0x3f3f3f3f;int parent[maxn];//有多少個邊int num; //ans數組中儲存的是升序排序的入樹的邊的權值double ans[maxn];struct point{ int x; int y;} p[maxn];struct ds{ int u; int v; double w;} d[maxn * maxn + maxn];double dist(const point& a, const point& b){ return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + 1.0 * (a.y - b.y) * (a.y - b.y));}bool cmp(const ds &a, const ds& b){ return a.w < b.w;}//並查集中找到父節點的操作int Find(int x){ //遞迴? //parent[x] = Find(parent[x]); //return parent[x]; //自己寫的迭代 int tmp = x; while(parent[tmp] != tmp) { tmp = parent[tmp]; } return tmp;}void merge(int x, int y){ int fa = Find(x); int fb = Find(y); if(fa != fb) { parent[fa] = fb; }}void init(){ scanf("%d%d",&n,&m); num = 0; for(int i = 0 ; i < m ; i++) { scanf("%d%d",&p[i].x,&p[i].y); } for(int i = 0; i < m ; i++) for(int j = i + 1; j < m ; j++) { d[num].u = i; d[num].v = j; d[num++].w = dist(p[i],p[j]); } for(int i = 0 ; i <= m ; i++) parent[i] = i; //從小到大進行排序 sort(d,d+num,cmp);}//最主要的代碼:Kruskalvoid kruskal(){ int cnt = 0 ; for(int i = 0 ; i < num ; i++) { int fa = Find(d[i].u); int fb = Find(d[i].v); if(fa != fb) { merge(d[i].u,d[i].v); ans[cnt++] = d[i].w; } } printf("%.2f\n",ans[cnt - n]);}int main(){ int t; scanf("%d",&t); while(t--) { init(); kruskal(); } return 0;}
注意點:在poj提交的時候對double型的應該用%f\n而不要用%lf\n
poj2349 Arctic Network - 最小產生樹