POJ 1789 Truck History "Prim"

Source: Internet
Author: User
Tags lowercase

Truck history

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 21736 Accepted: 8459

Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks is used for vegetable delivery, and for furniture, or for bricks. The company have its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (all letter in each position have a very special meaning bu T is the unimportant for this task). At the beginning of company's history, just a single truck type is used but later other types were derived from it, then From the new types another types were derived, and so on.

Today, ACM is a rich enough to pay historians to study it history. One thing historians tried to find out are so called derivation plan – i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type is derived from exactly one other truck type (except for the first truck type whic H is not derived from any other type). The quality of a derivation plan was and then defined as
1/σ (TO,TD) d (TO,TD)
Where the sum goes over all pairs of types in the derivation plan such that T o are the original type and T d the type Deri Ved from it and D (T o,t D) are the distance of the types.
Since historians failed, you is to the write a program to the help them. Given The codes of truck types, your program should find the highest possible quality of a derivation plan.

Input the input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= n <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). Assume that the codes uniquely describe the trucks, i.e., No. Of these N lines is the same. The input is terminated with zero at the place of number of truck types.

Output for each test case, your program should output the text "the highest possible quality are 1/q", where 1/q is the Qu Ality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

Source CTU Open 2003

Topic link.


Main topic:

With the development of the flow of goods, we can use different types of trucks.
Each truck company has its own number to describe different types of trucks, where the number consists of 7 lowercase letters (each letter has a special meaning in its own position, but not important for this task).

At the beginning of the company, there was only one truck type number, and then the truck number came from it.


ACM wants to learn the history of the company and see how the truck type number is developed.

Suppose that the distance of two different types of vans (translated for distance better understanding) are:

Two truck number, corresponding to the location, the number of different letters.

For example:

A car number is: AAAAAAA

B Car number: BAAAAAA

C Car number: ABAAAAA

So the distance between A and B is: Dis (b) =1,dis (a,c) =1,dis (b,c) = 2.

We can assume that, in addition to the initial numbering, each of the other numbers comes from another number.


The quality of the plan is defined as: 1/σ (TO,TD) d (TO,TD)

Which is the reciprocal of the sum of distance.

The highest quality of the derived plan.

The numerator is 1, the smaller the denominator, the larger the score, and vice versa.

So what we're asking for is the minimum distance sum.

That is, the minimum spanning tree is obtained.

Because the source number (root node) can be arbitrary, the other nodes as long as the line can be reached.

In addition to the source number, other numbers can be generated by the source number, or can be generated by any other number.


The minimum spanning tree can be obtained directly with Pirm.


#include <cstdio> #include <cstring> const int INF = 0X3F3F3F3F;
const int MAXN = 2010;
Char str[maxn][10];
int MAP[MAXN][MAXN],DIS[MAXN];
BOOL MARK[MAXN];
int n;
	int weight (int ti,int TJ) {int k,count=0;
	for (K=0;k<7;++k) if (str[ti][k]!=str[tj][k]) count++;
return count;
	} void init () {int i,j;	memset (map,inf,sizeof (map));
Because it is a full graph, it can be uninitialized (used vertices will be marked) for (I=1;i<n;++i) for (j=i+1;j<=n;++j) map[i][j]=map[j][i]=weight (I,J);
	} void Prim () {int i,j,minm,id,sum;
	memset (Mark,0,sizeof (Mark));
	for (I=1;i<=n;++i) {dis[i]=map[1][i];
	} mark[1]=true;
	sum=0;
		for (i=1;i<n;++i) {minm=inf;
				for (J=1;J&LT;=N;++J) {if (Dis[j]<minm&&!mark[j]) {minm=dis[j];
			Id=j;
		}} SUM+=MINM;
		Mark[id]=true;
		for (J=1;J&LT;=N;++J) {if (Dis[j]>map[id][j]&&!mark[j]) dis[j]=map[id][j];
}} printf ("The highest possible quality is 1/%d.\n", sum);
	} int main () {int i; while (scanf ("%d", &n), N) {for (i=1;i<=n;++i) scanf ("%s", Str[i]);
		Init ();
	Prim ();
} 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.