Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 32474 | Accepted: 12626 |
Description
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
Output
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
Source
#include <iostream>
#include <cstring>
using namespace std;
char car[2005][8];
int mp[2005][2005]; int prim(int n){
int dis[2005], visit[2005] = {0};
memset(dis, 0x3f, sizeof(dis));
dis[0] = 0;
visit[0] = 1;
for(int i = 1; i < n; i++){
dis[i] = mp[i][0];
}
for(int i = 0; i < n - 1; i++){
int min = 0x3f3f3f3f;
int p = -1;
for(int j = 1; j < n; j++){
if(visit[j] == 0 && min > dis[j]){
min = dis[j];
p = j;
}
}
if(p == -1){
return -1; //不连通
}
visit[p] = 1;
for(int i = 1; i < n; i++){
if(visit[i] == 0 && dis[i] > mp[p][i]){
dis[i] = mp[p][i];
}
}
}
int sum = 0;
for(int i = 0; i < n; i++)
sum += dis[i];
return sum;
} int main(){
// std::ios::sync_with_stdio(false);
int n;
while(cin >> n && n){
memset(mp, 0x3f, sizeof(mp));
for(int i = 0; i < n; i++){
cin >> car[i];
int sum = 0;
for(int j = 0; j < i; j++){
sum = 0;
for(int k = 0; k < 7; k++){
if(car[j][k] != car[i][k]){
sum++;
}
}
mp[i][j] = mp[j][i] = sum;
}
}
int rt = prim(n);
cout << "The highest possible quality is 1/";
cout << rt;
cout << "." << endl;
}
return 0;
}