# Course Design Problems R: tap water pipeline, course design tap water

Source: Internet
Author: User

Course Design Problems R: tap water pipeline, course design tap water
Description

You got a job to build a tap water pipeline on campus. There are several water supply points on campus, each of which may have multiple laying paths. The cost of each laying path is predictable.

The task requires that the final pipeline be laid to ensure that any two points can be connected directly or indirectly, and the total cost is the lowest.

Input

Each test case consists of multiple rows. The first row is two integers, P and R. P represents the number of points of Water Supply (1 <= P <= 50 ), each vertex corresponds to a unique number from 1 to P. R indicates the number of possible laying paths, and the number of paths may be very large. Next there is the R row. The format of each row is as follows:

Node A No. Node B No. Path cost

The path cost cannot exceed 100.

There is a blank line between test cases. The input ends with P = 0. Note that there is no R value.

Output

Each test case occupies the lowest total cost of one output line.

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`
`P points: Give the distance between the two points, and find the cost required for the Minimum Spanning Tree.Train of Thought: the cruise card algorithm. Sort the given edge by weight and traverse all edges. If the two vertices of the edge are not connected, add them to the Spanning Tree.Sort weights using the sort function. The key is to determine whether the two points are connected. I used the idea of querying sets. If two points are connected, the End Node of one point points to the End Node of another point (using array B for storage, B [I] indicates the next node that node I points ).Initialize B [I] = I first. Use the User-Defined Function f (I) to find the end node of the I node. If two nodes a and B f (a) and f (B) do not want to wait. Note that a, B, and two nodes are not connected.`

`# Include <cstdio >#include <algorithm> using namespace std; typedef struct {int v ;} p; // used to store the distance between two water supply points bool comp (p a, p B) // compare the elements in v  {return. v  <B. v ;} p a ; int B ; int f (int I) // find the final node {if (B [I] = I) return I;Else return f (B [I]); // It should be optimized to return B [I] = f (B [I]);} int main () {int I, t, n, s, j; while (scanf ("% d", & n) = 1 & n) {s = 0; scanf ("% d ", & t); for (I = 1; I <= t; I ++) scanf ("% d", & a [I]. v , & a [I]. v , & a [I]. v ); sort (a, a + t, comp); for (I = 1; I <= n; I ++) B [I] = I; for (I = 1; I <= t; I ++) {if (f (a [I]. v )! = F (a [I]. v ) // Add to the Spanning Tree if it is not connected and accumulate the path {s + = a [I]. v ; B [f (a [I]. v )] = f (a [I]. v ); // point the End Node of one point to the End Node of another} printf ("% d \ n", s);} return 0 ;}`

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.