Truck History(卡車歷史)

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 26547   Accepted: 10300

Descriptionios

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 t o is the original type and t d the type derived from it and d(t o,t d) 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. 

Inputthis

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.

Outputspa

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 Inputcode

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Outputblog

The highest possible quality is 1/3.

Sourceip

題意:
給你一個n;
n個長爲7的字符串;
每一個字符串表示一個節點,每一個節點向其餘全部點都有邊,邊長爲兩個節點字符串同一位置不一樣字符的數量;
須要你生成最短路的邊權和。
代碼實現(prim):
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 const int maxn=2010;
 6 const int inf=maxn*7;
 7 int n;
 8 int a[maxn],map[maxn][maxn];
 9 char ch[maxn][10];
10 bool v[maxn];
11 int bj(int x,int y){
12     int ret=0;
13     for(int i=0;i<7;i++)
14     if(ch[x][i]!=ch[y][i]) ret++;
15     return ret;
16 }
17 void intn(){
18     for(int i=1;i<=n;i++) scanf("%s",ch[i]);
19     for(int i=1;i<=n;i++)
20     for(int j=i+1;j<=n;j++)
21     map[j][i]=map[i][j]=bj(i,j);
22 }
23 int prim(){
24     memset(v,0,sizeof(v));
25     int ans=0,p,b;
26     v[1]=1;
27     for(int i=1;i<=n;i++) a[i]=map[1][i];
28     for(int m=1;m<n;m++){
29         p=inf;
30         for(int i=1;i<=n;i++) if(a[i]<p&&!v[i]){p=a[i];b=i;}
31         ans+=a[b];v[b]=1;
32         for(int i=1;i<=n;i++) a[i]=min(a[i],map[b][i]);
33     }
34     return ans;
35 }
36 int main(){
37     while(scanf("%d",&n)){
38         if(!n) break;
39         else intn();
40         printf("The highest possible quality is 1/%d.\n",prim());
41     }
42     return 0;
43 }

以前的prim打的醜,poj一直給我TLE(只有TLE),果真poj逼格這麼高的地方不適合我這樣不會英語,又很嬌弱的蒟蒻。ci

題目來源:POJ字符串

相關文章
相關標籤/搜索