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. wireshark添加用户执行

    默认情况下,访问网络端口需要root权限,而wireshark的只是/usr/share/dumpcap的一个UI,/usr/share/dumpcap需要root权限,所以没法non-root用户无 ...

  2. Struts2文件配置 登陆页面

    Struts 版本号 struts-2.3.16.3 web.xml 配置 <?xml version=”1.0″ encoding=”UTF-8″?> <web-app versi ...

  3. 纯JS写出日历

    封装代码: (function(cmf){ cmf.showDcalendar=function(){ var fnname=cmf.fn var id="cmfrili2" va ...

  4. Python【第十篇】协程、异步IO

    大纲 Gevent协程 阻塞IO和非阻塞IO.同步IO和异步IO的区别 事件驱动.IO多路复用(select/poll/epoll) 1.协程 1.1协程的概念 协程,又称微线程,纤程.英文名Coro ...

  5. smali 语法基础

    dalvik字节码有两种类型,原始类型和引用类型.对象和数组是引用类型,其它都是原始类型. V  void Z  boolean B  byte S  short C  char I  int F   ...

  6. Django1.7.1设置TEMPLATE_DIRS

    首先附上我的django工程目录结构: mysite│ db.sqlite3│ manage.py│├─mysite   │ settings.py   │ urls.py   │ views.py  ...

  7. 原生js实现吸顶导航和回到顶部特效

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. PAT (Basic Level) 1004. 成绩排名 (20)

    读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...

  9. ASP.NET(C#)常用数据加密和解密方法汇总

    一.            数据加密的概念 1.  基本概念 2.  基本功能 3.  加密形式 二.            数据加密的项目应用和学习 1.  媒体加密:DRM 2.  文件加密:文本 ...

  10. Unity与IOS交互

    Unity IOS交互 @By 广州小龙  QQ群:63438968 环境:Mac os 10.9.2  Unity 4.2.1f4   Xcode 5.0.2 Unity IOS的交互我写过一个教程 ...