Data Structure _ Course Design -- Minimum Spanning Tree: indoor cabling, course design room
* *********************************** Please specify the source: bytes ********************************************
It takes a lot of time to design this course, which is too troublesome =. (Obviously, the capability is insufficient)
~~~~ Minimum Spanning Tree: indoor cabling ~~~~
Question requirements:
Building a new house is a complicated project. Now you need to write a program to help the homeowner design the layout of indoor wires.
First, the wall socket is fixed. The sockets must be connected with wires and arranged neatly and beautifully. Each line must be parallel to at least one wall and embedded into four walls or floors (not on the roof ).
The homeowner asks to know the shortest length of the wire he needs to buy to connect all the sockets.
In addition, do not forget that each room has a door, and the wires cannot pass through the door. The layout of a room with 4 sockets is provided.
Input requirements:
The input consists of several groups of test data.
The 1st rows of data in each group contain the room length, width, height, and number of sockets N (N is a positive integer not greater than 20 ).
In the next N rows, line I provides the coordinates (xi, yi, zi) of the I socket. the last row contains four 3 tuples (x1, y1, z1)... (X4, y4, z4), which are the three-dimensional coordinates of the four corners of the rectangular door frame. If all four digits are 0, all tests are completed. Do not process the data.
Note: assume that the rectangular room is located in the first quadrant of the three-dimensional Cartesian coordinate system, and there is a corner on the origin. The floor is located in the x-y plane. Subject data guarantee: Each socket only belongs to one side of the four walls, and there is no socket on the door. The two ends of each wire segment must be connected only to the socket, and the wires cannot be cross-welded.
Output requirements:
For each group of tests, output the shortest integer length to connect all sockets to the desired wire in one row.
Input example:
10 10 10 4
0 1 3.3
2.5 0 2
5 0 0.8
5 10 1
0 0 0 0 0 3 1.5 0 1.5 0 3
0 0 0 0
Output example:
21
Pay attention to the following points for this question:
① The wiring should be parallel to the wall
② Relationship between two outlet locations
3 lines can go through the ground, not the roof or door
④ Final data, rounded up
Then, the question is the minimum spanning tree, but I spent a lot of time finding the distance between two sockets = ..
In my code comments, there are four faces, namely, 1, 3, and 4. We are facing face 1, and its left side is face 2, the right side of Area 1 is Area 3, and area 1 is facing area 4.
Then, the program is:
/*************************************** **************************************** * ******** Author: tree ** From: blog.csdn.net/lttree ** Title: Minimum Spanning Tree: indoor cabling ** Source: Data Structure _ Course Design ** Hint: minimum Spanning Tree ************************************* **************************************** * **********/# include <iostream> # include <algorithm> # include <math. h> using namespace std;/******* definition of some related variables ****** // The maximum number of sockets The value is 20. Therefore, there are up to 400 edges # define MAX 401 // The struct Node {double x, y, z;} nd [21], door [4]; // struct of Edge with weight value struct Edge {int u; int v; double quan;} eg [MAX]; // N-Number of sockets, len, wid, hei-length, width, and height of the room. The position of the pos_door door is int N, len, wid, hei, pos_door; int father [21]. /****** determine the relationship between the two sockets ****** // determine whether the two sockets are in the bool isTogether (Node a, Node B) {if (. x = B. x & (. x = 0 |. x = len) | (. y = B. y & (. y = 0 |. y = wid) return true; return false ;} // Determine whether the bool isBeside (Node a, Node B) {if (. x = 0 |. x = len) {if (B. y = 0 | B. y = wid) return true; elsereturn false;} else if (. y = 0 |. y = wid) {if (B. x = 0 | B. x = len) return true; elsereturn false; }}// whether the bool is0000ss (Node a, Node B) {if (. x = 0 & B. x = len) | (. y = 0 & B. y = wid) | (. x = len & B. x = 0) | (. y = wid & B. y = 0) return true; elsereturn false;}/******* a series of judgments ****** /// Calculate the minimum value of double Min (double a, double B) {return a <B? A: B;} // determine the wall where the door is located. int judge_d (Node d []) {if (d [0]. y = 0 & d [3]. y = 0) return 1; else if (d [0]. x = 0 & d [3]. x = 0) return 2; else if (d [0]. x = len & d [3]. x = len) return 3; else if (d [0]. y = wid & d [3]. y = wid) return 4;} // determine whether two sockets on the same wall are connected through bool judge_crossdoor (Node n1, Node n2) {// If the socket is at the bottom, or the outlet is positioned above the door, so it does not pass through the door (regardless of the relationship between the wall and the outlet location) if (n1.z = 0 | n2.z = 0 | n1.z> = door [3]. z | n2.z> = door [3]. z) return false; if (pos_doo R = 1) {if (n1.y! = 0 & n2.y! = 0) return false; if (n1.x> = door [3]. x & n2.x> = door [3]. x) | (n1.x <= door [0]. x & n2.x <= door [0]. x) return false; return true;} else if (pos_door = 2) {if (n1.x! = 0 & n2.x! = 0) return false; if (n1.y <= door [0]. y & n2.y <= door [0]. y) | (n1.y> = door [3]. y & n2.y> = door [3]. y) return false; return true;} else if (pos_door = 3) {if (n1.x! = Len & n2.y! = Len) return false; if (n1.y <= door [0]. y & n2.y <= door [0]. y) | (n1.y> = door [3]. y & n2.y> = door [3]. y) return false; return true;} else {if (n1.y! = Wid & n2.y! = Wid) return false; if (n1.x> = door [3]. x & n2.x> = door [3]. x) | (n1.x <= door [0]. x & n2.x <= door [0]. x) return false; return true ;}} /****** calculate the wiring length ***** // calculate the shortest wiring of the two sockets on the same wall. double find_togcost (Node a, Node B) {// two sockets with the same wall and no door if (! Judge_crossdoor (a, B) return (fabs (. x-b.x) + fabs (. y-b.y) + fabs (. z-b.z); else {// two sockets wiring will pass through the door, the door location is different if (pos_door = 1 | pos_door = 4) return Min (fabs (. x-b.x) + fabs (door [3]. z-a.z) + fabs (door [3]. z-b.z), (fabs (. x-b.x) +. z + B. z); elsereturnMin (fabs (. y-b.y) + fabs (door [3]. z-a.z) + fabs (door [3]. z-b.z), (fabs (. y-b.y) +. z + B. z) ;}}// calculate the shortest wiring of the Two wall sockets double find_acrcost (Node a, Node B) {double cost1, cost2; Node Temp1, temp2; // if (. y = 0 & B. y = wid) | (B. y = 0 &. y = wid) {// calculate the right value of if (pos_door = 1) returnMin (Min (. y + fabs (door [3]. z-a.z) + len + B. y), (. y +. z + len + B. y), Min (wid-a.y + len + wid-b.y), (. z + len + B. z); else if (pos_door = 2) {temp1 = temp2 = a; temp1.y = 0, temp2.y = wid; cost1 = find_togcost (a, temp1 ); cost2 = find_togcost (a, temp2); returnMin (cost1 + len + B. y), (cost2 + len + wid-b.y);} else if (pos_doo R = 3) {temp1 = temp2 = B; temp1.y = 0, temp2.y = wid; cost1 = find_togcost (B, temp1); cost2 = find_togcost (B, temp2 ); returnMin (cost1 + len +. y), (cost2 + len + wid-a.y);} elsereturnMin (Min (. y + len + B. y), (wid-a.y + wid-b.y + fabs (door [3]. z-a.z) + len), Min (wid-a.y + wid-b.y +. z + len), (. z + B. z + len);} else {if (pos_door = 1) {temp1 = temp2 = a; temp1.x = 0, temp2.x = len; cost1 = find_togcost (, temp1); cost2 = find_togcost (a, temp2); returnM In (cost1 + wid + B. x), (cost2 + wid + len-b.x);} else if (pos_door = 2) return Min (. x + B. x + wid + fabs (door [3]. z-a.z), (. x + B. x + wid +. z), Min (len-a.x + len-b.x + wid), (. z + B. z + wid); else if (pos_door = 4) {temp1 = temp2 = B; temp1.x = 0, temp2.x = len; cost1 = find_togcost (B, temp1 ); cost2 = find_togcost (B, temp2); return Min (cost1 + wid +. x), (cost2 + wid + len-a.x);} else return Min (. x + B. x + wid), (. z + B. z + wid), M In (len-a.x + len-b.x + fabs (door [3]. z-a.z) + wid), (len-a.x + len-b.x +. z + wid) ;}}// calculate the shortest wiring of the Two wall sockets double find_bescost (Node a, Node B) {Node temp =; // find a point in the Two-plane connection (let one point x and y be 0) and convert it into two same wall sockets if (. x = 0 & B. y = 0) | (B. x = 0 &. y = 0) {temp. x = temp. y = 0; return (find_togcost (a, temp) + find_togcost (B, temp);} else if (. x = len & B. y = 0) | (. y = 0 & B. x = len) {temp. x = len, temp. y = 0; return (find_togcost (A, temp) + find_togcost (B, temp);} else if (. x = 0 & B. y = wid) | (B. x = 0 &. y = wid) {temp. x = 0, temp. y = wid; return (find_togcost (a, temp) + find_togcost (B, temp);} else {temp. x = len, temp. y = wid; return (find_togcost (a, temp) + find_togcost (B, temp) ;}/ ****** find the Minimum Spanning Tree (Kruscal) * ***** // comparison function bool cmp (Edge e1, Edge e2) {return e1.quan <e2.quan;} // query the void Init (int m) function of the Set initialization Function) {int I; for (I = 1; I <= m; I ++) fathe R [I] = I;} // query the set query function int Find (int x) {while (father [x]! = X) x = father [x]; return x;} // returns the void Combine (int a, int B) {int temp_a, temp_ B; temp_a = Find (a); temp_ B = Find (B); if (temp_a! = Temp_ B) father [temp_a] = temp_ B;} // The double Kruskal (int n) {Edge e; int I; double res; sort (eg, eg + n, cmp); // check the set to initialize Init (N); // build the Minimum Spanning Tree res = 0; for (I = 0; I <n; ++ I) {e = eg [I]; if (Find (e. u )! = Find (e. v) {Combine (e. u, e. v); res + = e. quan ;}} return res ;}/ ******* main function ******/void main () {// I, j is the intermediate variable, k is the number of edges int I, j, k; while (cin> len> wid> hei> N) {// the input data is 4 0, exit the program if (! Len &&! Wid &&! Hei &&! N) break; // (outlet and door location) for (I = 0; I <N; ++ I) cin> nd [I]. x> nd [I]. y> nd [I]. z; for (I = 0; I <4; ++ I) cin> door [I]. x> door [I]. y> door [I]. z; pos_door = judge_d (door);/* calculate the distance between two points (note that the wiring should be parallel to the wall) */k = 0; for (I = 0; I <N; ++ I) {for (j = I + 1; j <N; ++ j) {eg [k]. u = I; eg [k]. v = j; // determine the relationship between two points. The same wall or adjacent wall or relative wall // same wall if (isTogether (nd [I], nd [j]) eg [k]. quan = find_togcost (nd [I], nd [j]); // relative wall else if (isw.ss (nd [I], nd [j]) eg [k]. quan = find_acrcost (nd [I], nd [j]); // adjacent wall elseeg [k]. quan = find_bescost (nd [I], nd [j]); ++ k ;}}/* use the Kruscal algorithm to find the Minimum Spanning Tree * // pay attention to the final result, regardless of the length, double cost; cost = Kruskal (k); if (cost-int (cost) = 0) cout <cost <endl; elsecout <int (cost + 1) <endl ;}}
* *********************************** Please specify the source: bytes ********************************************
Data Structure Course Design -- construct the minimal spanning tree that can connect n cities
Use Prim. I have contact information in my documents.
Urgent, Data Structure course design uses the Kruskal algorithm to generate the minimum tree
Consider a city as a point and a distance between cities as a weight between points.
The following is the Minimum Spanning Tree Code implemented by the PRIM algorithm ., Edge information is stored using the adjacent matrix. The program has been compiled and can be run directly.
# Include <stdio. h>
# Include <string. h>
Typedef int VRType;
Typedef char InfoType;
# Define MAX_NAME 3
/* The maximum length of the vertex string + 1 */
# Define MAX_INFO 20
/* Maximum length of the related information string + 1 */
Typedef char VertexType [MAX_NAME];
# Define INFINITY 32767
/* Replace infinity with the maximum integer value */
# Define MAX_VERTEX_NUM 20
/* Maximum number of vertices */
Typedef enum GraphKind;
/**/
Typedef int PathMatrix [MAX_VERTEX_NUM] [MAX_VERTEX_NUM];
Typedef int character pathtable [MAX_VERTEX_NUM];
Typedef struct
{
VRType adj;
/* Vertex link type. For a no-Permission graph, 1 (yes) or 0 (NO) indicates adjacent No */
/* For a full map, the value type */
InfoType * info;
/* Pointer of the arc-related information (optional )*/
} ArcCell, AdjMatrix [MAX_VERTEX_NUM] [MAX_VERTEX_NUM];
Typedef struct
{
VertexType vexs [MAX_VERTEX_NUM];
/* Vertex vector */
AdjMatrix arcs;
/* Adjacent matrix */
Int vexnum, arcnum;
/* Number of vertices and arcs of the graph */
GraphKind kind;
/* Chart type flag */
} MGraph;
Int LocateVex (MGraph G, VertexType u)
{/* Initial condition: Graph G exists, and vertices in u and G share the same features */
/* Operation result: If vertex u exists in G, the vertex is returned in the graph; otherwise,-1 */is returned */
Int I;
For (I = 0; I <G. vexnum; ++ I)
If (strcmp (u, G. vexs [I]) = 0)
Return I;
Return-1;
}
Int CreateAN (MGraph * G)
{/* Uses array (Adjacent matrix) notation to construct an undirected network G */
Int I, j, k, w, IncInfo;
Char s [MAX_INFO], * info;
VertexType va, vb;
Printf ("please input number of acmes and arcs in G, and there is some information in arc, if yes is 1, else is 0 :");
Scanf ("% d, % d, % d", & (* G). vexnum, & (* G). arcnum, & IncInfo );
Printf ("please input the value of % d acmes (<% d cha... full text>