You are on page 1of 3

4/12/2016

10369ArcticNetwork(UVa)|ASDFCoding

ASDFCoding
Programmigwithpassion

10369ArcticNetwork(UVa)
byafruizc
The problem statement can be found here. (hp://uva.onlinejudge.org/index.php?
option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=1310)
ProblemDescription:
Someoutpostsneedtobeconnectedviawireless.Therearesomeoutpoststhathavesatellite
signals.Thosethathavesatellitesignalscancommunicateoversatellite(nokidding)regardless
oftheirlocation.Thosethatdonthavesatellite,havetocommunicateviaradiosignals.Allthe
network of outposts needs to be connected so you need to nd the maximum distance D
betweentwospecicoutposts,sothatthecostofconnectingtheentirenetworkisminimum.
ThesmallerDis,thecheaperthenetworkwouldbe.
Analysis:
As you can see, there is something going on with graphs here. And if they say that all the
network should be connected and with minimal cost, mmmm that suspicious. So, are you
ponderingwhatImpondering,probablyyoualreadyknowthisisanMSTproblem.Thesubtle
point(andthekeytosolvethisproblem)istotakeintoaccountthesatellitesignals.Ifweare
given2satellitesignals,thatmeans,therecanbetwocomponentsofthegraph,becausethey
canconnectwitheachotherviasatellite,whichwontconsumeanyradiodistance.So,thereal
problem to solve here is more commonly known as: Minimum Spanning Forest. Its called
forest, because there is not just one component in the graph. There is more than one. The
numberofconnectedcomponentswewanttoachieveisthenthenumberofsatellitesignalswe
have,andthisisbecauseeveryconnectedcomponentcanhaveoneofthesatellitesignals,thus
ensuringtheconnectivityamongalltheoutposts.
Solution:
ThesolutionistomodifyKruskalsalgorithmalilebit.Youneedtostartjoiningcomponents
untilyoureachthenumberofcomponentsyouneed.InthiscaseS,orthenumberofsatellite
signals.Onceyoujoinapairofsignalsyoushouldtestwetherthenewdistanceisgreaterthan
thepreviousmaximumdistancepreviouslystored.Ifthisisthecase,changethenewdistance.
After repeating this process, youll have the maximum distance among all the minimum
distancesneededtocommunicateallthenetworks.
Implementation:
1

#include<cmath>

#include<vector>

https://asdfcoding.wordpress.com/2012/12/17/10369arcticnetworkuva/

1/3

4/12/2016

10369ArcticNetwork(UVa)|ASDFCoding

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include<vector>
#include<cstdio>
#include<algorithm>

usingnamespacestd;

typedefpair<int,int>ii;
typedefvector<int>vi;

vipset(500);
int_size;

voidinitSet(intN){
for(inti=0;i<N;i++)
pset[i]=i;
_size=N;
}
intfindSet(inti){
returnpset[i]==i?i:(pset[i]=findSet(pset[i]));
}
boolisSameSet(inti,intj){
returnfindSet(i)==findSet(j);
}
voidunionSet(inti,intj){
if(!isSameSet(i,j)){
pset[findSet(i)]=findSet(j);
_size;
}
}
intgetSize(){
return_size;
}

intT,S,P,x[500],y[500];
vector<pair<double,ii>>EdgeList;

intmain(){
scanf("%d",&T);
while(T){
EdgeList.clear();
scanf("%d%d",&S,&P);
for(inti=0;i<P;i++){
scanf("%d%d",&x[i],&y[i]);
}

for(inti=0;i<P;i++)
for(intj=i+1;j<P;j++){
doubledist=hypot((double)x[i]x[j],(double)y[i]y[j]);
EdgeList.push_back(make_pair(dist,ii(i,j)));
}

sort(EdgeList.begin(),EdgeList.end());
initSet(P);
doubleD=0.0;
for(inti=0;getSize()>S;i++){
pair<double,ii>front=EdgeList[i];
if(!isSameSet(front.second.first,front.second.second)){

59

D=max(D,front.first);

https://asdfcoding.wordpress.com/2012/12/17/10369arcticnetworkuva/

2/3

4/12/2016

59
60
61
62
63
64
65
66

10369ArcticNetwork(UVa)|ASDFCoding

D=max(D,front.first);
unionSet(front.second.first,front.second.second);
}
}

printf("%.2lf\n",D);
}
}

PUBLISHED:20121217(20121217T22:37:56+0000)
FILEDUNDER:CompetitiveProgramming,Graphs,MinimumSpanningTree
TAGS:CompetitiveProgramming:grafos
BlogatWordPress.com.TheManifestTheme.

https://asdfcoding.wordpress.com/2012/12/17/10369arcticnetworkuva/

3/3

You might also like