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

链接: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();
}


内容概要:本文档详细介绍了基于事件触发扩展状态观测器(ESO)的分布式非线性车辆队列控制系统的实现。该系统由N+1辆车组成(1个领头车和N个跟随车),每辆车具有非线性动力学模型,考虑了空气阻力、滚动阻力等非线性因素及参数不确定性和外部扰动。通过事件触发ESO估计总扰动,基于动态面控制方法设计分布式控制律,并引入事件触发机制以减少通信和计算负担。系统还包含仿真主循环、结果可视化等功能模块。该实现严格遵循论文所述方法,验证了观测误差有界性、间距误差收敛性等核心结论。 适合人群:具备一定编程基础,对非线性系统控制、事件触发机制、扩展状态观测器等有一定了解的研发人员和研究人员。 使用场景及目标:①研究分布式非线性车辆队列控制系统的理论与实现;②理解事件触发机制如何减少通信和计算负担;③掌握扩展状态观测器在非线性系统中的应用;④学习动态面控制方法的设计与实现。 其他说明:本文档不仅提供了详细的代码实现,还对每个模块进行了深入解析,包括非线性建模优势、ESO核心优势、动态面控制与传统反步法对比、事件触发机制优化等方面。此外,文档还实现了论文中的稳定性分析,通过数值仿真验证了论文的核心结论,确保了系统的稳定性和有效性。建议读者在学习过程中结合代码进行实践,并关注各个模块之间的联系与相互作用。
内容概要:本文档《c预约面试大全.pdf》汇集了大量C语言及其相关领域的面试问题与解答,涵盖了从基础概念到高级技巧的广泛知识点。主要内容包括但不限于:C语言的基础语法(如static关键字的作用、指针与引用的区别)、数据结构(如平衡二叉树、链表操作)、算法(如冒泡排序的时间复杂度)、计算机网络(如TCP/IP协议栈)、操作系统(如进程和线程的区别)、内存管理(如堆栈差异)等方面。此外,还涉及了一些较为复杂的主题,例如位域的应用、编译原理中的预编译概念、以及针对特定问题的编程实现(如寻找数组中的重复数字、实现约瑟夫环问题等)。每个问题都配有详细的解释或代码示例,旨在帮助求职者全面准备C语言相关的技术面试。 适合人群:正在准备C语言及相关领域(如嵌入式开发、系统编程)工作的求职者,尤其是有一定编程基础但缺乏实战经验的技术人员。 使用场景及目标:①帮助读者深入理解C语言的核心概念和技术细节;②通过实际案例分析提升解决复杂问题的能力;③为参加各类技术面试做好充分的知识储备和心理准备。 其他说明:此文档不仅包含了理论知识,还提供了大量实战练习的机会,鼓励读者动手实践,从而更好地掌握所学内容。同时,文档中的问题难度逐步递增,适合不同层次的学习者按需选择。由于C语言是许多高级编程语言的基础,因此这份资料对于想要深入学习计算机科学的学生也非常有价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值