Truck History
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 10998 |
|
Accepted: 4092 |
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.
1次AC就是爽撒。
題目中生詞太多了,有點看不懂,不過還好,還是懂了
就是有n輛車,每輛車都有一個7位的編號,兩個編號之間的d代表這兩個編號之間不同字母的個數。
一個編號只能由另一個編號“衍生”出來,代價是這兩個編號之間相應的d,
現在要找出一個“衍生”方案,使得總代價最小,也就是d之和最小。
怎么做呢
以任意兩個車的編號作為節點,這條邊的邊權為兩個編號間不同字母的個數,
這樣圖就建好了,至于用prim還是用kruskal,就無所謂了
我用的prim,矩陣來存圖,好費空間啊
這題確實水了,連我都能1A
1
#include<stdio.h>
2
#include<string.h>
3
#include<math.h>
4
#define MAX 2100
5
char s[MAX][7];
6
int map[MAX][MAX];
7
int n,ans,cost[MAX];
8
short vis[MAX];
9
int cmp(char s1[],char s2[])
10

{
11
int i,tot;
12
tot=0;
13
for (i=0; i<=6 ; i++ )
14
if (s1[i]!=s2[i]) tot++;
15
return tot;
16
}
17
void init()
18

{
19
int i,j;
20
for (i=1; i<=n ; i++ )
21
scanf("%s",&s[i]);
22
memset(map,0,sizeof(map));
23
for (i=1; i<=n-1 ; i++ )
24
for (j=i+1; j<=n; j++)
25
{
26
map[i][j]=cmp(s[i],s[j]);
27
map[j][i]=map[i][j];
28
}
29
}
30
void prim()
31

{
32
int i,j,mini,min;
33
memset(vis,0,sizeof(vis));
34
for (i=2; i<=n; i++) cost[i]=map[1][i];
35
vis[1]=1;
36
ans=0;
37
for (i=1; i<=n-1 ; i++ )
38
{
39
min=0x7fffffff;
40
for (j=1; j<=n ; j++ )
41
if ((!vis[j])&&(cost[j]<min))
42
{
43
min=cost[j];
44
mini=j;
45
}
46
ans=ans+min;
47
vis[mini]=1;
48
for (j=1; j<=n ; j++ )
49
if ((!vis[j])&&(map[mini][j]>0)&&(map[mini][j]<cost[j]))
50
cost[j]=map[mini][j];
51
}
52
}
53
int main()
54

{
55
scanf("%d",&n);
56
while (n!=0)
57
{
58
init();
59
prim();
60
printf("The highest possible quality is 1/%d.\n",ans);
61
scanf("%d",&n);
62
}
63
return 0;
64
}
65