# 1789: Truck History

Advanced Cargo Movement, Ltd. uses trucks of differenttypes. Some trucks are used for vegetable delivery, other for furniture, or forbricks. The company has its own code describing each type of a truck. The codeis simply a string of exactly seven lowercase letters (each letter on eachposition has a very special meaning but that is unimportant for this task). Atthe beginning of company's history, just a single truck type was used but laterother types were derived from it, then from the new types another types werederived, and so on.

Today, ACM is rich enough to pay historians to study its history. One thinghistorians tried to find out is so called derivation plan -- i.e. how the trucktypes were derived. They defined the distance of truck types as the number ofpositions with different letters in truck type codes. They also assumed thateach truck type was derived from exactly one other truck type (except for thefirst truck type which was not derived from any other type). The quality of aderivation 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 isthe 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 thecodes of truck types, your program should find the highest possible quality ofa derivation plan.

The input consists of several test cases. Each test casebegins with a line containing the number of truck types, N, 2 <= N <= 2000. Each of the following N lines of input contains one truck type code (astring of seven lowercase letters). You may assume that the codes uniquelydescribe the trucks, i.e., no two of these N lines are the same. The input isterminated with zero at the place of number of truck types.

For each test case, your program should output the text"The highest possible quality is 1/Q.", where 1/Q is the quality ofthe best derivation plan.

4

aaaaaaa

baaaaaa

abaaaaa

aabaaaa

0

The highest possible quality is 1/3.

-----------------------------------------------------

Prime算法求最小支撑树

-----------------------------------------------------

//G:Truck History
//总时间限制: 2000ms 内存限制: 65536kB
//描述
//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.
//输入
//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.
//输出
//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.
//样例输入
//4
//aaaaaaa
//baaaaaa
//abaaaaa
//aabaaaa
//0
//样例输出
//The highest possible quality is 1/3.

// Prim算法求最小支撑树

#include<fstream>
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<cstring>
#include<limits.h>
using namespace std;

const int MAX = 2001;

int mat[MAX][MAX] = {};
int weight[MAX] = {};
bool mark[MAX] = {};

int main()
{
#ifndef ONLINE_JUDGE
ifstream fin("tm201601G.txt");
int n,i,j,ii,len,cnt,mincnt,argmin,ans;
string s;
vector<string> vec;
while(fin >> n)
{
if (n==0)
{
break;
}
vec.clear();
for (i=0; i<n; i++)
{
fin >> s;
vec.push_back(s);
}
len = s.size();
// 建图
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
cnt = 0;
for (ii=0; ii<len; ii++)
{
if (vec.at(i).at(ii)!=vec.at(j).at(ii))
{
cnt++;
}
}
mat[i][j] = cnt;
mat[j][i] = cnt;
}
}
// Prim算法
memset(mark,false,sizeof(mark));
memset(weight,INT_MAX,sizeof(weight));
mark[0] = true;
for (i=1; i<n; i++)
{
weight[i] = mat[0][i];
}
ans = 0;
for (ii=0; ii<n-1; ii++)
{
mincnt = INT_MAX;
for (i=0; i<n; i++)
{
if (!mark[i] && weight[i]<mincnt)
{
mincnt = weight[i];
argmin = i;
}
}
mark[argmin] = true;
ans += mincnt;
// weight: 第i个点到已经生成的点集C的最短距离
// 向点集C中加入新的点后,更新weight的值
// 采用weight将原来的O(n3)的复杂度降为O(n2)
for (i=0; i<n; i++)
{
if (!mark[i] && mat[argmin][i]<weight[i])
{
weight[i] = mat[argmin][i];
}

}

}
cout << "The highest possible quality is 1/" << ans << "." << endl;
}
fin.close();
#endif
#ifdef ONLINE_JUDGE
int n,i,j,ii,len,cnt,mincnt,argmin,ans;
string s;
vector<string> vec;
while(cin >> n)
{
if (n==0)
{
break;
}
vec.clear();
for (i=0; i<n; i++)
{
cin >> s;
vec.push_back(s);
}
len = s.size();
// 建图
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
cnt = 0;
for (ii=0; ii<len; ii++)
{
if (vec.at(i).at(ii)!=vec.at(j).at(ii))
{
cnt++;
}
}
mat[i][j] = cnt;
mat[j][i] = cnt;
}
}
// Prim算法
memset(mark,false,sizeof(mark));
memset(weight,INT_MAX,sizeof(weight));
mark[0] = true;
for (i=1; i<n; i++)
{
weight[i] = mat[0][i];
}
ans = 0;
for (ii=0; ii<n-1; ii++)
{
mincnt = INT_MAX;
for (i=0; i<n; i++)
{
if (!mark[i] && weight[i]<mincnt)
{
mincnt = weight[i];
argmin = i;
}
}
mark[argmin] = true;
ans += mincnt;
// weight: 第i个点到已经生成的点集C的最短距离
// 向点集C中加入新的点后,更新weight的值
// 采用weight将原来的O(n3)的复杂度降为O(n2)
for (i=0; i<n; i++)
{
if (!mark[i] && mat[argmin][i]<weight[i])
{
weight[i] = mat[argmin][i];
}

}

}
cout << "The highest possible quality is 1/" << ans << "." << endl;
}
#endif
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120