poj 2349 Arctic Network
Arctic Network
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 5390 | Accepted: 1909 |
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.
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
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
求最小生成树中第k长的边,对结果加个排序就可以了
用kruskal算法对稠密图还是比较慢的,一会儿再用prim试试
#include<iostream> #include<algorithm> #include<cmath> using namespace std; struct node { int x, y; double weight; }; node edge[2500100]; int father[510]; int num; double ans[250000]; int mapx[510]; int mapy[510]; int n, p; int cur; int comp(node a, node b) { return a.weight<b.weight; } int find_set(int a) { if(father[a] != a) { father[a] = find_set(father[a]); } return father[a]; } void union_set(int a, int b) { father[a] = b; } void make_set() { for(int i = 0; i<p;i++) father[i] = i; } void kruskal() { int i; cur = 0; for(i=0;i<num;i++) { int pa = find_set(edge[i].x); int pb = find_set(edge[i].y); if(pa != pb) { union_set(pa, pb); ans[cur]=edge[i].weight; cur++; } } } int main() { int cases; int i, j; freopen("e:\\data.txt", "r", stdin); freopen("e:\\out.txt", "w", stdout); cin>>cases; while(cases--) { cin>>n>>p; for(i=0;i<p;i++) { cin>>mapx[i]>>mapy[i]; } num = 0; for(i=0;i<p;i++) { for(j=0;j<p;j++) { if(i>j) { double dis = sqrt((mapx[i]-mapx[j])*(mapx[i]-mapx[j])*1.0+1.0*(mapy[i]-mapy[j])*(mapy[i]-mapy[j])); edge[num].x = i; edge[num].y = j; edge[num].weight = dis; num++; } } } sort(edge, edge+num, comp); make_set(); kruskal(); sort(ans, ans+cur); printf("%.2f\n",ans[cur-n]); } return 0; }
然后再尝试用prim算法写一遍
以后注意要初始化为无穷。。。
#include<iostream> #include<algorithm> #include<cmath> using namespace std; double map[510][510]; int mapx[510]; int mapy[510]; double ans[510]; double d[510]; int mark[510]; int n, p; int cur; const double INF = 99999999.0; void prim() { int i, j; double mini; int pos; for(i = 1; i <= p; i++) { d[i] = map[1][i]; } mark[1] = 1; d[1] = 0; for(i = 1; i <= p - 1; i++) { mini = INF; pos = 0; for(j = 1; j <= p; j++) { if(mark[j] != 1 && d[j] < INF && d[j] < mini) { mini = d[j]; pos = j; } } ans[cur] = mini; cur++; mark[pos] = 1; for(j = 1; j <= p; j++) { if(mark[j] != 1 && map[pos][j] < d[j] && map[pos][j] < INF) d[j] = map[pos][j]; } } } int main() { int cases; int i, j; freopen("e:\\data.txt", "r", stdin); freopen("e:\\out.txt", "w", stdout); cin>>cases; while(cases--) { cin>>n>>p; for(i = 1; i <= p; i++) { cin>>mapx[i]>>mapy[i]; } for(i = 1; i <= p; i++) { for(j = 1; j <= p; j++) { map[i][j] = INF; } } for(i = 1; i <= p; i++) { d[i] = INF; } for(i = 1; i <= p; i++) { for(j = 1; j <= p; j++) { if(i > j) { double dis = sqrt((mapx[i] - mapx[j]) * (mapx[i]-mapx[j]) * 1.0
+ 1.0 * (mapy[i] - mapy[j]) * (mapy[i] - mapy[j])); map[i][j] = dis; map[j][i] = dis; } } } memset(mark, 0, sizeof(mark)); memset(ans, 0, sizeof(ans)); cur = 0; prim(); sort(ans, ans + cur); printf("%.2f\n", ans[cur - n]); } return 0; }
浙公网安备 33010602011771号