POJ 1789 Truck History 圖論 prim算法 最小生成樹

Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15326   Accepted: 5886

Description

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

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is 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 was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as
1/Σ(to,td)d(to,td)

where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) is the distance of the types.
Since historians failed, you are to write a program to 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). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are 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 is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

這道題不用看,最小生成樹,prim算法,不同卡車是節點,然後密碼相錯多少是路徑,直接先寫一個函數,判定路徑長度,任意兩點距離,即任意兩種卡車密碼錯位數多少,圖建立起來就是將算法帶入,在最小生成樹prim函數裏面加一步,求出任意路徑算出路徑的長度並且全部加起來,返回這個值,直接輸出就可以AC了,還是比較簡單,難度就是修改prim函數。再基本木有難度了。。。

下面是AC代碼:

#include<cstdio>
#include<iostream>
#include<cstring>
#define infinity 1000000
#define max_vertexes 2012
using namespace std;
int G[max_vertexes][max_vertexes];
char ch[2012][10];
int prim(int vcount)
{
    int i,j,k,sum=0;
    int lowcost[max_vertexes],closeset[max_vertexes],used[max_vertexes];
    for (i=0;i<vcount;i++)
        {
        lowcost[i]=G[0][i];
        closeset[i]=0; 
        used[i]=0; 
        }
    used[0]=1; 
    for (i=1;i<vcount;i++)
        {
        j=0;
        while (used[j]) j++;
        for (k=0;k<vcount;k++)
            if ((!used[k])&&(lowcost[k]<lowcost[j])) j=k;
		sum+=G[j][closeset[j]];
        used[j]=1;
        for (k=0;k<vcount;k++)
            if (!used[k]&&(G[j][k]<lowcost[k]))
                { lowcost[k]=G[j][k];
                closeset[k]=j; }
        }
	return sum;
}
void init(int n)
{
	int i,j,k;
	for(i=0;i<n;i++)
		for(j=i+1;j<n;j++)
			for(k=0;k<7;k++)
				if(ch[i][k]!=ch[j][k])
				{
					G[i][j]++;
				    G[j][i]++;
				}
}
int main()
{
	int n,i;
	while(1)
	{
		scanf("%d",&n);
		if(n==0)
			break;
		memset(G,0,sizeof(G));
		for(i=0;i<n;i++)
			scanf("%s",ch[i]);
		init(n);
		printf("The highest possible quality is 1/%d.\n",prim(n));
	}
	return 0;
}

今天主頁君很高興,在POJ上面AC了三道題,並且都是1A的,很高興啊!!!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章