Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27572 | Accepted: 10717 |
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
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.
這道題主要是看懂題意比較難,有車然後其他的車都是從已有的車型中衍生出來,每個車都是由7個字符組成,然後求出最少的車的距離,大概就是用prim算法衍生出來就行了。
然後偷偷瞄了一下別人的代碼,發現這個方法理解起來最方便
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
char str[2000][8];
int dis[2000][2000];
int lowdis[2000];
int main()
{
int n ;
while ( cin >> n , n )
{
int i , j , k ;
for ( i = 0 ; i < n ; i ++ )
cin >> str [i];
memset(dis,0,sizeof(dis));
for ( i = 0 ; i < n - 1 ; i ++ )
{
for ( j = i+1 ; j < n ; j ++ )
{
for ( k = 0 ; k < 7 ; k ++ )
{
if(str[i][k]!=str[j][k])
dis[i][j]++;
}
dis[j][i]=dis[i][j];
}
}
for ( i = 0 ; i < n ; i ++ )
lowdis[i]=dis[0][i];
int ans = 0 ;
for ( i = 0 ; i < n - 1 ; i ++ )
{
int mindis = 99999999 ;
for ( j = 0 ; j < n ; j ++ )
{
if( lowdis[j] && mindis > lowdis[j] )
{
mindis = lowdis[j] ;
k = j;
}
}
ans += mindis ;
lowdis[k] = 0 ;
for ( j = 0 ; j < n ; j ++ )
{
if(lowdis[j]>dis[k][j])
lowdis[j]=dis[k][j];
}
}
cout<<"The highest possible quality is 1/"<<ans<<"."<<endl;
}
return 0;
}