Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21518   Accepted: 8367

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.

我也是挺纳闷 为什么我把 建图和prime算法过程 写作子函数放在main函数外边怎么就不能调用呢?
题意:给出卡车的编号,编号是一个长度为7的字符串,一个编号由另一个编号衍生出来,而衍生出来所需要的代价就是两个编号之间不同字符的个数
(相当于两个顶点相连后的权值),现在求给出的这组字符串所需要的最小代价
#include<stdio.h>
#include<string.h>
#define MAX 2010
#define INF 0x3ffffff
int map[MAX][MAX];
char s[MAX][11];
int low[MAX],vis[MAX];
int t;
int fun(int i,int j)
{
int ans=0;
for(int k=0;k<7;k++)
{
if(s[i][k]!=s[j][k])
ans++;
}
return ans;
}
void init()
{
int i,j;
for(i=0;i<t;i++)
{
for(j=0;j<t;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=INF;
}
}
}
int main()
{
int t;
int i,j;
while(scanf("%d",&t),t)
{
init();
for(i=0;i<t;i++)
scanf("%s",s[i]);
for(i=0;i<t-1;i++)
{
for(j=i+1;j<t;j++)
{
map[i][j]=map[j][i]=fun(i,j);
}
}
int next,min,mindis=0;
memset(vis,0,sizeof(vis));
for(i=0;i<t;i++)
low[i]=map[0][i];
vis[0]=1;
for(i=0;i<t-1;i++)
{
min=INF;
for(j=0;j<t;j++)
{
if(!vis[j]&&min>low[j])
{
min=low[j];
next=j;
}
}
mindis+=min;
vis[next]=1;
for(j=0;j<t;j++)
{
if(!vis[j]&&low[j]>map[next][j])
low[j]=map[next][j];
}
}
printf("The highest possible quality is 1/%d.\n",mindis);
}
return 0;
}

  

poj 1789 Truck History【最小生成树prime】的更多相关文章

  1. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

  2. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

  3. POJ 1789 -- Truck History(Prim)

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

  4. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  5. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  6. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

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

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

  8. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

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

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

随机推荐

  1. macbook pro retina 编程字体推荐

    使用VS2010.VS2012.Qt Creator编译工具首推等宽字体,等宽字体中consolas. 首先大家都知道等宽对于编码来说的直观性不言而喻,其次retina屏幕的特殊性,整天用特别小的字体 ...

  2. Nodejs简单验证码ccap安装

    首先要求: node npm 安装时如果提示npm-gyp失败,可进行如下操作: 确认python版本2.7+ 安装npm install ccap 如果失败,尝试npm install ccap@0 ...

  3. Windowsphone 之xml序列化和反序列化的应用(WebService解析返回的数据DataSet )

    关于Xml的序列化和反序列化: 可以看这篇文章,http://www.cnblogs.com/Windows-phone/p/3243575.html WebService解析返回的数据DataSet ...

  4. c#配置文件appStrings配置节的读取、添加和修改

    程序开发中经常会用到应用程序配置文件,好处就是维护人员可以直接修改配置文件进行维护,而不用修改程序.好,切入主题. 给项目添加应用程序配置文件App.config,先在里面写几句: <?xml ...

  5. java 正则操作之获取

    // 正则操作 获取import java.util.regex.*;class Demo{ public static void main(String[] args){  String str=& ...

  6. 调用相册怎么设置剪裁-b

    //创建一个相册控制器 UIImagePickerController *pc = [[UIImagePickerController alloc] init]; //图片来源// UIImagePi ...

  7. 金山网络2014春季Android实习生招聘-成都站-笔试第二题

    一个文件名为input.txt的文件当中,每一行都有一个单词,要求统计单词出现的频率,并且按照从小到大出现次数打印,次数相同的按照首字母顺序排序. package jinshanwangluo.exa ...

  8. [BZOJ 3620] 似乎在梦中见过的样子 【KMP】

    题目链接:BZOJ - 3620 题目分析 这道题使用 KMP 做 O(n^2) 的暴力就能过. 首先,我们依次枚举字串左端点 l ,然后从这个左端点开始向后做一次 KMP. 然后我们枚举右端点 r  ...

  9. Java学习IO篇

            来吧,同志们,为复习网络编程做准备-- 一.理论准备         流是个抽象的概念,是对输入输出设备的抽象,Java程序中,对于数据的输入/输出操作都是以"流" ...

  10. 也说说EM

    也说说EM [本文链接:http://www.cnblogs.com/breezedeus/archive/2012/08/12/2634466.html,转载请注明出处] 前几天看Andrew Ng ...