UvaOJ10369 - Arctic Network

 1 /*
 2     The first line of each test case contains 1 <= S <= 100, the number of satellite channels!
 3     注意:S表示一共有多少个卫星,那么就是有 最多有S-1个通道! 然后将最小生成树中的后边的 S-1通道去掉就行了!
 4     思路:最小生成树中的第 k 个最小边!
 5 */
 6 //克鲁斯克尔算法.....
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<cstring>
10 #include<algorithm>
11 #include<cmath>
12 using namespace std;
13
14 double x[800], y[800];
15
16 struct node{
17    int u, v;
18    double d;
19 };
20
21 bool cmp(node a, node b){
22     return a.d < b.d;
23 }
24
25 int f[505];
26
27 node nd[150000];
28 double ret[505];
29
30 int getFather(int x){
31    return x==f[x] ? x : f[x]=getFather(f[x]);
32 }
33
34 bool Union(int a, int b){
35    int fa=getFather(a), fb=getFather(b);
36    if(fa!=fb){
37        f[fa]=fb;
38        return true;
39    }
40    return false;
41 }
42
43 int main(){
44    int n, m;
45    int t;
46    scanf("%d", &t);
47    while(t--){
48           scanf("%d%d", &m, &n);
49        for(int i=1; i<=n; ++i){
50           scanf("%lf%lf", &x[i], &y[i]);
51           f[i]=i;
52        }
53        int cnt=0;
54        for(int i=1; i<n; ++i)
55           for(int j=i+1; j<=n; ++j){
56               nd[cnt].u=i;
57               nd[cnt].v=j;
58               nd[cnt++].d=sqrt( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));
59           }
60        sort(nd, nd+cnt, cmp);
61        int cc=0;
62        for(int i=0; i<cnt; ++i)
63           if(Union(nd[i].u, nd[i].v))
64              ret[cc++]=nd[i].d;
65         for(int i=0; i<cc; ++i)
66            cout<<ret[i]<<"fdsf"<<endl;
67        printf("%.2lf\n", ret[n-m-1]);
68    }
69    return 0;
70 }
 1 //prim算法.......
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 const double INF = 0x3f3f3f3f*1.0;
 9 double x[800], y[800];
10
11 int n, m;
12 double map[505][505];
13 int vis[505];
14
15 double ret[505];
16
17 void prim(){
18     memset(vis, 0, sizeof(vis));
19     vis[1]=1;
20     for(int i=2; i<=n; ++i)
21        ret[i]=INF;
22     int root=1, p;
23     for(int i=1; i<n; ++i){
24         double minLen=INF;
25         for(int j=2; j<=n; ++j){
26            if(!vis[j] && ret[j]>map[root][j])
27               ret[j]=map[root][j];
28            if(!vis[j] && minLen>ret[j]){
29               minLen=ret[j];
30               p=j;
31            }
32         }
33         root=p;
34         vis[root]=1;
35     }
36 }
37
38 int main(){
39
40    int t;
41    scanf("%d", &t);
42    while(t--){
43           scanf("%d%d", &m, &n);
44        for(int i=1; i<=n; ++i)
45           scanf("%lf%lf", &x[i], &y[i]);
46        for(int i=1; i<n; ++i)
47           for(int j=i+1; j<=n; ++j)
48              map[i][j]=map[j][i]=sqrt( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));
49
50        prim();
51        sort(ret, ret+n+1);
52
53        printf("%.2lf\n", ret[n-m+1]);
54    }
55    return 0;
56 }

UvaOJ10369 - Arctic Network

时间: 2024-11-05 14:46:31

UvaOJ10369 - Arctic Network的相关文章

POJ 2349 Arctic Network

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18447   Accepted: 5829 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Arctic Network (最小生成树第K大(小)边)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13108   Accepted: 4256 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ2032 Arctic Network(Prim)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16968   Accepted: 5412 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

UVA 10369 Arctic Network

题目来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1310 最小生成树问题,Prim算法在这种给出坐标的情况相对Kruskal算法优势还是很大. 1 /*AC 19ms*/ 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<math.h> 5 #

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

Arctic Network (poj 2349 最小生成树)

Language: Default Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12397   Accepted: 4053 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different

G - Arctic Network

G - Arctic Network #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 510 using namespace std; double an; int fa[MAXN]; int t,n,m,tot,sum; double x[MAXN],y[MAXN],ans[MAXN];

[Poj2349]Arctic Network(二分,最小生成树)

[Poj2349]Arctic Network Description 国防部(DND)要用无线网络连接北部几个哨所.两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道. 任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置.同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率.功率越大,D也越大,但成本更高.出于采购和维修的方便,所有哨所的收发器必须是相同的:那就是说,D值对每一个哨所相同. 你的任务是确定收发器的D的最