链接:http://poj.org/problem?id=1789 或 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1158
Description
Input
Output
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
分析: 要使派生方案的优劣值最大,那么分母的值当然是要越小越好,而且要求考虑所有类型对(t0, td)的距离,使得最终派生方案中每种卡车类型都是由其他一种卡车类型派生出来的(除了最初的卡车类型之外)这样,将每种卡车类型理解成一个无向网中的顶点,所要求的最佳派生方案就是求最小生成树,而上述表达式中的分母就是最小生成树的权值;
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#define MAXN 2005
#define INF 0x1f1f1f1f
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;
int n, res, cas, dist;
int lowcost[MAXN], d[MAXN][MAXN];
char code[MAXN][10];
int Min_Tree()
{
RST(d), res = 0;
for(int i=0; i<n; i++) {
for(int j=i+1; j<n; j++) {
dist = 0;
for(int k=0; k<7; k++) {
dist += code[i][k]!=code[j][k];
}
d[i][j] = d[j][i] = dist;
}
}
lowcost[0] = -1;
for(int i=1; i<n; i++) lowcost[i] = d[0][i];
for(int i=1; i<n; i++) {
int min = INF, p;
for(int j=0; j<n; j++) {
if(lowcost[j] != -1 && lowcost[j] < min) {
p = j;
min = lowcost[j];
}
}
res += min;
lowcost[p] = -1;
for(int j=0; j<n; j++) {
if(d[p][j] < lowcost[j]) lowcost[j] = d[p][j];
}
}
return res;
}
void Init()
{
for(int i=0; i<n; i++) scanf("%s", code[i]);
printf("The highest possible quality is 1/%d.\n", Min_Tree());
}
int main()
{
while(~scanf("%d", &n) && n) Init();
}ZOJ 2158 && POJ 1789 Truck History (经典MST),布布扣,bubuko.com
ZOJ 2158 && POJ 1789 Truck History (经典MST)
原文:http://blog.csdn.net/keshacookie/article/details/24998653