Topic Link: ZOJ1372 POJ 1287 Networking Network design
Networking Time limit: 2 Seconds Memory Limit: 65536 KB
You is assigned to the design network connections between certain points in a wide area. You is given a set of points in the area, and a set of possible routes for the cables, which may connect pairs of points. For each possible route between and points, you were given the length of the cable that's needed to connect the points ove R that route. Note that there may exist many possible routes between, given points. It is assumed, the given possible routes connect (directly or indirectly) each of the points in the area.
Your task is to design the network for the area, so there is a connection (direct or indirect) between every TS (i.e., all the points is interconnected, but not necessarily by a direct cable), and which the total length of the used Cable is minimal.
Input
the input file consists of a number of data Sets. Each data set defines one required network. The first line of the set contains integers:the first defines the number P of the given points, and the second the Nu Mber R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers:the first and numbers Identify the points, and the third gives the length of the route. The numbers is separated with white spaces. A data set giving only one number p=0 denotes the end of the input. The data sets is separated with a empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes is identified with integers between 1 and P (inclusive). The routes between II points I and J may be given as I J or as J I.
Output
For each data set, print one number in a separate line that gives the total length of the cable used for the entire design Ed Network.
Sample Input
1 0
2 3
1 2 37
2 1 17
1 2 68
3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32
5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12
0
Sample Output
0
17
16
26
Test Instructions:
Design a network connection to some of the points of an area, as well as the lines that can be connected between these sites by network cables. For each line, the length of the network cable required to connect the two places is given. Note that there may be more than one route between two locations. It is assumed that a given line can connect directly or indirectly to all locations in the region. Try to design a network system for this area so that all locations in the area can be connected and the cable length is the shortest.
Analysis:
The minimum spanning tree, which uses the Kruskal algorithm, avoids the problem of heavy edges, because the edges are ordered from small to large, so that longer edges are not selected when the shorter edge is selected.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace STD; #define MAXM 1300int parent[55];int ans, m;struct edge{ int u, V, W;} Eg[maxm];bool CMP (Edge A, Edge b) { return A.W < B.W;} int Find (int x) { if (parent[x] = =-1) return x; Return Find (Parent[x]);} void Kruskal () { memset (parent,-1, sizeof (parent)); Ans = 0; Sort (EG, eg+m, CMP); for (int i = 0; i < m; i++) { int t1 = find (eg[i].u), t2 = find (EG[I].V); if (t1! = t2) { ans + = EG[I].W; PARENT[T1] = t2;}} } int main () { int n; while (scanf ("%d", &n), N) { scanf ("%d", &m); for (int i = 0; i < m; i++) scanf ("%d%d%d", &eg[i].u, &EG[I].V, &EG[I].W); Kruskal (); printf ("%d\n", ans); } return 0;}
ZOJ1372 POJ 1287 Networking network design Kruskal algorithm