Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31871   Accepted: 12427

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.
题意:
给你一个n;
n个长为7的字符串;
每个字符串表示一个节点,每个节点向其他所有点都有边,边长为两个节点字符串同一位置不同字符的数量;
需要你生成最短路的边权和。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 2010
#define M 10 char str[N][M];//放字符串
int n;//结点数
int vis[N],dst[N],map[N][N]; //vis访问数组,dst放各点到MST的最小距离 int finddst(int i,int j)//找两字符串字符不同的列数
{
int num=0,k; for (k=0;k<7;k++)
{
if(str[i][k]!=str[j][k])
{
num++;
}
}
return num;
} void init()
{
int j,i; memset(vis,0,sizeof(vis));//访问数组初始
for (i=0;i<n;i++)//初始化图
{
for (j=0;j<n;j++)
{
if (i==j)
{
map[i][j]=0;
} map[i][j]=finddst(i,j);
}
}
} void prime()
{
int i,j,min,point,ans=0; vis[0]=1;//0点放入MST
for (i=0;i<n;i++)//dst初始化
{
dst[i]=map[i][0];
} for (i=1;i<n;i++)
{
min=N;
for (j=0;j<n;j++)//找距MST最近的点
{
if (vis[j]==0&&min>dst[j])
{
min=dst[j];
point=j;
}
} if (min==N)//不连通情况
{
break;
} vis[point]=1;//把该点放入MST
ans=ans+dst[point]; for (j=0;j<n;j++)//更新各点到MST的最小距离
{
if (vis[j]==0&&dst[j]>map[point][j])
{
dst[j]=map[point][j];
}
}
} printf("The highest possible quality is 1/%d.\n",ans);
} int main()
{
int i,j; while (scanf("%d",&n)&&n)
{
for (i=0;i<n;i++)
{
scanf("%s",&str[i]);
} init();
prime();
} return 0;
}

Truck History(prime)的更多相关文章

  1. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  2. Truck History(最小生成树)

    poj——Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27703   Accepted: 10 ...

  3. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

  4. poj 1789 Truck History(最小生成树)

    模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...

  5. 【POJ 1789】Truck History(最小生成树)

    题意:距离定义为两个字符串的不同字符的位置个数.然后求出最小生成树. #include <algorithm> #include <cstdio> #include <c ...

  6. History(历史)命令用法

    如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的用法. 使用 HISTTIMEFORMAT 显示时间 ...

  7. History(历史)命令用法15例

    导读 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率,本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFOR ...

  8. [转] Linux History(历史)命令用法 15 例

    [From]https://linuxtoy.org/archives/history-command-usage-examples.html 如果你经常使用 Linux 命令行,那么使用 histo ...

  9. History(历史)命令用法 15 例

    如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFORMAT ...

随机推荐

  1. Pig join用法举例

    jnd = join a by f1, b by f2;   join操作默认的是内连接,只有两边都匹配才会保留   需要用null补位的那边需要知道它的模式: 如果是左外连接,需要知道右边的数据集的 ...

  2. HAproxy simple

    下载地址 start install: wget     http://www.haproxy.org/download/1.7/src/haproxy-1.7.5.tar.gz tar   -xf  ...

  3. Linux 运维工程师一定要知道的六类好习惯和23个教训

    一.线上操作规范 1.测试使用当初学习Linux的使用,从基础到服务到集群,都是在虚拟机做的,虽然老师告诉我们跟真机没有什么差别,可是对真实环境的渴望日渐上升,不过虚拟机的各种快照却让我们养成了各种手 ...

  4. ExpressRoute 线路和路由域

    你必须订购一条 ExpressRoute 线路 ,以通过连接提供商将你的本地基础结构连接到 Azure.下图提供了你的 WAN 与 Azure 之间的连接的逻辑表示形式. ExpressRoute 线 ...

  5. iOS设计模式 - 责任链

    iOS设计模式 - 责任链 原理图 说明 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...

  6. Linux less/more命令详解

    less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻, 只能往后面看,但若使用了 less 时,就可以使用 [pageup] [pagedown] 等按键的功能来 ...

  7. Hadoop HBase概念学习系列之HBase的Shell(步骤非常清晰)(二十四)

    这部分知识点,是必须要熟练玩转的! 见 5 hbase-shell + hbase的java api 的进入HBase Shell   强烈建议,先看我上面的这篇博文,是实实际际的步骤. 另外,附上一 ...

  8. 对象.isdigit() ,只能判断全是数字的字符串

    s = "55p"" print(s.isdigit()) # False s2 = "5568" print(s2.isdigit()) # Tru ...

  9. Array对象常用方法

    不改变原数组: 1.concat()  连接两个或多个数组 不改变原数组 返回被连接数组的一个副本 2.join()  把数组中所有元素放入一个字符串 不改变原数组 返回字符串 3.slice()   ...

  10. 操作系统的三个接口 shell gui api

    操作系统的三个接口 shell gui api 编程语言是用来告诉操作系统干什么的语言. 编程语言是人机交互语言. 程序.进程:任务集.