首页 > 其他 > 详细

ZOJ 2158 && POJ 1789 Truck History (经典MST)

时间:2014-05-10 09:38:13      阅读:483      评论:0      收藏:0      [点我收藏+]

链接:http://poj.org/problem?id=1789 或  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1158

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.

分析: 要使派生方案的优劣值最大,那么分母的值当然是要越小越好,而且要求考虑所有类型对(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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!