Networking
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 5976 |
|
Accepted: 3231 |
Description
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 02 31 2 372 1 171 2 683 71 2 192 3 113 1 71 3 52 3 893 1 911 2 325 71 2 52 3 72 4 84 5 113 5 101 5 64 2 120
Sample Output
0171626
Test instructions
P points, r to edge, find the minimum path and
Analysis:
The minimum spanning tree problem with length as the edge weight
Kruskal & Collection
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < vector> #include <queue> #define INF 0x3f3f3f3fusing namespace Std;const int maxn=5100;int r[maxn];int n, M; struct node{int x; int y; int cost;}; Node A[maxn];bool CMP (Node A, Node B) {return a.cost<b.cost;} void Init () {for (int i=0; i<n; i++) scanf ("%d%d%d", &a[i].x, &a[i].y, &a[i].cost); Sort (A, a+n, CMP); for (int i=0; i<n; i++) r[i] = i;} int find (int x) {return r[x]==x? x:r[x] = Find (R[x]);} void Kruskal () {int cnt = 0; int ans = 0; int x; int y; for (int i=0; i<n; i++) {x = find (a[i].x); y = Find (A[I].Y); if (x!=y) {cnt++; Ans + = a[i].cost; if (cnt = = m-1) break; R[y] = x; }} printf ("%d\n", ans);} int main () {while (scanf ("%d", &m) ==1 &&m) {SCANF ("%d", &n); Init (); Kruskal (); } return 0;}
POJ 1287:networking (minimum spanning tree Kruskal)