HDU 1233 or the smooth work of the project __ Good questions

Source: Internet
Author: User

/*
The main idea: his road has not been repaired, now must build a road, find out the most economical way to build a road, that is, to find the shortest distance can be.
The idea of solving problems: use and check the set, try to replace it. To see the least, is the greedy algorithm. Do not be afraid of greed, greed is to arrange a sequence, the order is good, you will succeed
Difficult to explain: the array to open to 5000 before it, or in the deposit of the time will appear cross-border phenomenon
Key points: Using and searching the collection and the flexible use of the collection
Solution Person: Lingnichong
Solution Time: 2014-08-12 23:56:43
Solution experience: The array is small, has been WA, the array is open for so large reason: First 100 of the village, but then the N (N-1)/2 lines correspond to the distance between the village, so the array to open very large. Summed up the wrong many times the main reason is that the use of the collection is not very familiar, there is not deep understanding of the problem.
*/


It's still a smooth project .    Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 25918 Accepted submission (s): 11544

Problem Description a province to investigate the rural traffic situation, the statistics obtained indicate the distance between any two villages. The goal of the provincial government's "smooth project" is to make road traffic (but not necessarily direct) between any two villages in the province, as long as it can be reached indirectly through highways, and the total length of the paved road is minimal. Please calculate the minimum total length of the road.

Input test entry contains several test cases. The 1th row of each test case gives the number of villages N (< 100), followed by N (N-1)/2 lines corresponding to the distance between villages, each row gives a positive integer, the number of two villages, respectively, and the distance between the two villages. For simplicity's sake, the village is numbered from 1 to N.
When n is 0 o'clock, the input ends and the use case is not processed.

Output for each test case, the minimum total length of the road is exported in 1 lines.

Sample Input

3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
Sample Output
3 5 Hint Hint Huge input, scanf is recommended.

#include <stdio.h>
#include <algorithm>
using namespace std;

int father[110]; 

struct place
{
	int start;
	int end;
	int D;
} A[5000];

int cmp (place x,place y)
{return
	x.d<y.d;
}

int find (int x)
{return
	x==father[x]?x:father[x]=find (father[x]);
}

int mergr (int s1,int s2)
{
	int fa=find (S1), fb=find (S2);
	if (FA!=FB)
	{
		father[fa]=fb;
		return 1;
	}
	return 0;
}

int main ()
{
	int n,i;
	int sum,m;
	while (scanf ("%d", &n), N)
	{
		sum=0;
		m=n* (n-1)/2;
		for (i=1;i<=m;i++)
			scanf ("%d%d%d", &A[I].START,&A[I].END,&A[I].D);
		Sort (a+1,a+1+m,cmp)//Less one plus one for 
		(i=1;i<=n;i++)
			father[i]=i;
		for (sum=0,i=1;i<=m;i++)//Here is M, because there are m group data 
			if (Mergr (a[i].start,a[i].end))
				sum+=a[i].d;
		printf ("%d\n", sum);	
	}
	return 0;





Contact Us

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.