1. 程式人生 > >POJ 1789

POJ 1789

Truck History

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 34106 Accepted: 13202

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

<span style="color:#000000">4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
</span>

Sample Output

<span style="color:#000000">The highest possible quality is 1/3.
</span>

Source

大致題意:

用一個7位的string代表一個編號,兩個編號之間的distance代表這兩個編號之間不同字母的個數。一個編號只能由另一個編號“衍生”出來,代價是這兩個編號之間相應的distance,現在要找出一個“衍生”方案,使得總代價最小,也就是distance之和最小。

例如有如下4個編號:

aaaaaaa

baaaaaa

abaaaaa

aabaaaa

顯然的,第二,第三和第四編號分別從第一編號衍生出來的代價最小,因為第二,第三和第四編號分別與第一編號只有一個字母是不同的,相應的distance都是1,加起來是3。也就是最小代價為3。

解題思路:

問題可以轉化為最小代價生成樹的問題。因為每兩個結點之間都有路徑,所以是完全圖。 

此題的關鍵是將問題轉化為最小生成樹的問題。

每一個編號為圖的一個頂點,頂點與頂點間的編號差即為這條邊的權值,

題目所要的就是我們求出最小生成樹來。這裡我用prim演算法來求最小生成樹。

程式碼:

//Memory Time 
//15688K 344MS 
 
#include<iostream>
#include<string>
using namespace std;
 
const int inf=10;          //無窮大(兩點間邊權最大為7)
const int large=2001;
 
int n;  //truck types
char str[large][8];
int dist[large][large]={0};
 
/*Compute Weight*/
 
int weight(int i,int j)     //返回兩個字串中不同字元的個數(返回邊權)
{
	int w=0;
	for(int k=0;k<7;k++)
		if(str[i][k]!=str[j][k])
			w++;
	return w;
}
 
/*Prim Algorithm*/
 
int prim(void)
{
	int s=1;       //源點(最初的源點為1)
	int m=1;       //記錄最小生成樹的頂點數
	bool u[large]; //記錄某頂點是否屬於最小生成樹
	int prim_w=0;  //最小生成樹的總權值
	int min_w;     //每個新源點到其它點的最短路
	int flag_point;
	int low_dis[large];  //各個源點到其它點的最短路
 
	memset(low_dis,inf,sizeof(low_dis));
	memset(u,false,sizeof(u));
	u[s]=true;
 
	while(1)
	{
		if(m==n)      //當最小生成樹的頂點數等於原圖的頂點數時,說明最小生成樹查詢完畢
			break;
 
		min_w=inf;
		for(int j=2;j<=n;j++)
		{
			if(!u[j] && low_dis[j]>dist[s][j])
				low_dis[j] = dist[s][j];
			if(!u[j] && min_w>low_dis[j])
			{
				min_w=low_dis[j];
				flag_point=j;      //記錄最小權邊中不屬於最小生成樹的點j
			}
		}
		s=flag_point;       //頂點j與舊源點合併
		u[s]=true;          //j點併入最小生成樹(相當於從圖上刪除j點,讓新源點接替所有j點具備的特徵)
		prim_w+=min_w;      //當前最小生成樹的總權值
		m++;                
	}
	return prim_w;
}
 
int main(void)
{
	int i,j;
 
	while(cin>>n && n)
	{
		/*Input*/
		
		for(i=1;i<=n;i++)
			cin>>str[i];
 
		/*Structure Maps*/
 
		for(i=1;i<=n-1;i++)
			for(j=i+1;j<=n;j++)
				dist[i][j]=dist[j][i]=weight(i,j);
 
		/*Prim Algorithm & Output*/
 
		cout<<"The highest possible quality is 1/"<<prim()<<'.'<<endl;
 
	}
	return 0;
}

同思路的程式碼:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 2010 ;
const int inf = 0x3f3f3f3f  ;
char str[maxn][10] ;
int map[maxn][maxn] ;
int vis[maxn] ;
int dis[maxn] ;
int n ;
int prime()
{
    for(int i = 2;i <= n;i++)
    dis[i] = inf ;
    dis[1] = 0 ;
    memset(vis , 0 ,sizeof(vis)) ;
    int ans = 0 ;
    while(1)
    {
        int mi = inf ;int pos ;
        for(int i = 1;i <= n;i++)
        if(!vis[i] && dis[i] < mi)
        mi = dis[pos = i] ;
        if(mi == inf)break;
        vis[pos] = 1;
        ans += mi ;
        for(int j = 1;j <= n ;j++)
        dis[j] = min(dis[j] , map[pos][j]) ;
    }
    return ans ;
}
int main()
{
    while(scanf("%d" , &n) && n)
    {
        for(int i = 1;i <= n;i++)
        {
            scanf("%s" , &str[i][1]) ;
            for(int j = 1;j <= i;j++)
            {
                int sum = 0 ;
                for(int k = 1;k <= 7 ;k++)
                if(str[i][k] != str[j][k])
                sum++ ;
                map[i][j] = map[j][i] = sum ;
            }
        }
        int ans = prime() ;
        printf("The highest possible quality is 1/%d.\n" , ans) ;
    }
    return 0 ;
}