Test instructions gives you the right to the minimum spanning tree by n points m Bar
This is the bare minimum spanning tree.
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N =, M = 3000; int Par[n], N, M, ans;struct edge{int u, V, W;} E[m];bool cmp (Edge A, Edge b) {return A.W < B.W;} int Find (int x) {int r = x, tmp; while (Par[r] >= 0) r = par[r]; while (x! = r) {tmp = par[x]; Par[x] = r; x = tmp; } return R;} void Union (int u, int v) {int ru = find (u), rv = Find (v), TMP = Par[ru] + PAR[RV]; if (Par[ru] < PAR[RV]) par[rv] = ru, par[ru] = tmp; else Par[ru] = RV, par[rv] = tmp;} void Kruskal () {memset (par,-1, sizeof (PAR)); int cnt = 0; for (int i = 1; I <= m; ++i) {int u = e[i].u, v = e[i].v; if (Find (u)! = Find (v)) {++cnt; Ans + = E[I].W; Union (U, v); } if (CNT >= n-1) break; }}int Main () {int u, V, W; while (scanf ("%d", &n), N) {scanf ("%d", &m); for (int i = 1; I <= m; ++i) {scanf ("%d%d%d", &u, &v, &w); e[i].u = u, e[i].v = V, e[i].w = w; } sort (e + 1, E + M + 1, CMP); Ans = 0; Kruskal (); printf ("%d\n", ans); } return 0;}
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
POJ 1287 Networking (minimum spanning tree)