Problem description
There are currently n wireless routers in a large flat room, each of which is fixed at a certain point. Any two wireless routers can establish a network connection to each other as long as the distance does not exceed R.
In addition, there are also m locations where the wireless router can be placed. You can select up to K to add a new router in these locations.
Your goal is to make the network connection between the 1th router and the 2nd router pass through as few brokered routers as possible. How much is the minimum number of relay routers in the optimal scheme?
Input format
The first line consists of four positive integer n,m,k,r. (2≤n≤100,1≤k≤m≤100, 1≤r≤108).
Next n rows, each line contains two integers, Xi and Yi, indicating that a wireless router has been placed at (xi, Yi) point. The input data ensures that the 1th and 2nd routers can already be connected to each other (through a series of brokered routers) with only these n routers.
Next m line, each line contains two integers xi and Yi, indicating (xi, Yi) point can be added to a router.
The absolute value of all coordinates in the input does not exceed 108, guaranteeing that the coordinates in the input are different.
Output format
The output has only one number, that is, the number of transit routers that pass through the 1th router to the 2nd router, with the addition of a K router in the specified location.
Input sample
5313
00
55
03
05
35
33
44
30
Output sample
2
The code is too long to write, as if the parameter option K is useless at all.
The use of the flood in the computer network idea, add a node (set as the first layer) then add all the nodes connected to it (set as the second layer), add a second layer of all nodes connected nodes (set to the third layer), if added to T, the number of layers must be minimal.
Please correct me if there is anything wrong.
1#include <iostream>2#include <vector>3#include <math.h>4 5 using namespacestd;6 7 classnode{//A node class that contains coordinate values, hierarchical values, and other node address vectors that are connected8 Private:9 intx;Ten inty; One intType//Originally to distinguish between the original route and the new route, it was found to be of no use but can be used as the hierarchical value of the diffusion method AVector<node*> v;//vector that stores the address of the connected node (routing) - - Public: theNode (intXintYinttype): X (x), Y (y), type (type) {} - - intGetX ()Const { - returnx; + } - + intGetY ()Const { A returny; at } - - voidSetType (inttype) { -Node::type =type; - } - in intGetType ()Const { - returntype; to } + - voidAddNode (Node *node) the { * v.push_back (node); $ }Panax Notoginseng - BOOLIsEqual (Node *n)//Determine if it is the same node the { + return This==N; A } the +vector<node*>* Getv ()//Returns the vector address that holds the address of the connected node - { $ return&v; $ } - }; - the BOOLIsreachable (Node &n1,node &n2,intDistance//Determine if two nodes can be connected - {Wuyi Doubledis = sqrt ((N1.getx ()-n2.getx ()) * (N1.getx ()-n2.getx ()) the+ (N1.gety ()-n2.gety ()) * (N1.gety ()-n2.gety ())); - returndis<=distance; Wu } - About intMain () { $ intN,m,k,r; -Vector<node*> original;//vector for storing input route node addresses -Vector<node*> Network;//vector used to add nodes to the network -Cin>>n>>m>>k>>R; A for(intI=0; i<n;i++)//Input The original node information, if connected with the joined nodes to add to each other's connection node vector + { the intx, y; -Cin>>x>>y; $Node *origin =NewNode (x, Y,0); the Original.push_back (origin); the for(intj=0; j<i;j++) the { the if(Isreachable (*original[j],*origin,r)) - { inOriginal[j]->AddNode (origin); theOrigin->AddNode (Original[j]); the } About the } the } theNode *s = original[0];//OK start node +Node *t = original[1];//Determine arrival node - for(intI=0; i<m;i++)//same loop, add optional node the {Bayi intx, y; theCin>>x>>y; theNode *addition =NewNode (x, Y,0); - Original.push_back (addition); - for(intj=0; j<n+i;j++) the { the if(Isreachable (*original[j], *addition, R)) { theOriginal[j]->AddNode (addition); theAddition->AddNode (Original[j]); - } the } the } theNetwork.push_back (s);//Add start node94 BOOLControl =true;//Set external loop control variables the for(intit=0, netsize=1; it<netsize && control;it++)//it is the subscript in the network vector, Netsize is the network vector size that will change, directly with. Size () always error = = the { thevector<node*>* Vv=network[it]->getv ();//Gets the adjacent node vector of the current node98 intSize = (int) vv->size (); About for(intj =0; j<size;j++) {//To determine all connected vectors - BOOLFlag =false;101 for(intI=0; I<network.size (); i++)102 {103 if(Network[i]->isequal (*VV) [j]))104 { theFlag =true;106 }107 }108 if(!flag)//If it is not present in the network, add109 { the(*VV) [J]->settype (Network[it]->gettype () +1);//Set the number of add nodes to the current node layer +1111Network.push_back ((*VV) [j]);//Add to Network thenetsize++;113 if(T->isequal (*VV) [j])//If you reach T, jump out of the inner Loop and terminate the outer loop the { theControl =false; the Break;117 }118 }119 } - }121Cout<<t->gettype ()-1<<endl;//output T number of layers-1122 return 0;123}
First CCF Real topic 4-Wireless network