POJ 1798 Truck History
Description
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 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
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
题意:输入1个n,代表有多少组字符数组。每一个数组都仅仅有7个字符,每2个数组不同字符的个数代表这2个字符串的距离(如果为距离)。如今要你构建一棵树,使得所花费的距离最小
思路:用Prim算法处理。
AC代码:
#include<stdio.h>
#include<string.h>
#define MAXN 100000000
int mark[2014],f[2014],n;
char str[2014][8];
int val(int i,int j) //2个字符串的距离
{
int sum,k;
sum=0;
for(k=0;k<7;k++)
if(str[i][k]!=str[j][k])
sum++;
return sum;
}
void prim()
{
int i,j,sum=0,minn,k;
for(i=0;i<n;i++) //每一个字符串与0的距离
f[i]=val(0,i);
memset(mark,0,sizeof(mark));
f[0]=0;
mark[0]=1; //标记已使用过0
for(i=0;i<n-1;i++) //Prim算法的核心,这个东西必需要自己理解!
{
minn=MAXN;
for(j=0;j<n;j++)
{
if(!mark[j]&&minn>f[j])
{
minn=f[j];
k=j;
}
}
sum+=minn;
mark[k]=1;
for(j=0;j<n;j++) //换一个顶点
if(!mark[j]&&f[j]>val(k,j))
f[j]=val(k,j);
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
for(i=0;i<n;i++)
scanf("%s",str[i]);
prim();
}
return 0;
}
POJ 1798 Truck History的更多相关文章
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
- POJ 1789 Truck History【最小生成树简单应用】
链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- poj 1789 Truck History【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- POJ 1789 Truck History (Kruskal 最小生成树)
题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...
随机推荐
- 解决Qt程序在Linux下无法输入中文的办法
解决Qt程序在Linux下无法输入中文的办法 一位网友问我怎样在Linux的Qt的应用程序中输入中文,我一開始认为不是什么问题,可是后面自己尝试了一下还真不行.不仅是Qt制作的应用程序,就连Qt Cr ...
- JavaBean在DAO设计模式简介
一.信息系统开发框架 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层是client,简单的来说就是浏览器. 2.显示层:JSP/Se ...
- 安卓开发28:自定义View类
自定义View类 通过自定义View类,可以自定义复杂的,按照自己需求的控件. 一个简单的例子 mainActivity.java 这个里面就是最普通的代码,但是给自定义的控件加上了一个onclick ...
- CI(codeigniter)框架,routes.php设置正确,但是显示服务器错误,是__construct少写了一个下划线
今天弄了一下CI框架,大概看了一下文档,感觉CI框架非常精简,但是在做的时候遇到了问题,CI文档中提供了一个新闻系统的例子,所有工作都做完了,在浏览器中打开相对应的url是,却显示“服务器错误”,一点 ...
- mysql导出和导入数据库
出口 在dos计划,切换到mysql按照该文件夹bin下一个.输入以下命令 mysqldump -u root -p nxu_life > nxu_life2.sql 运行完毕后,就能够看到在b ...
- JavaScript 中创建对象的方法(读书笔记思维导图)
面向对象(Object-Oriented, OO)的语言有一个标志,那就是它们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.而 ECMAScript 中没有类的概念,所以我们可以使用 ...
- centos 更改hostname
vim /etc/hosts vim /etc/sysconfig/network hostname hostname mlzboy-centos63
- Android菜鸟的成长笔记(27)——ViewPager的使用
ViewPager是Android 3.0以上能够使用的API. 一.ViewPager能干什么? 1.微信5.0中连带滑动用ViewPager能够轻松实现. 2.实现相似于新浪微博的导航引导界面. ...
- JDBC/XML的一些基本使用
原文:JDBC/XML的一些基本使用 一.知识点题目:JDBC核心API的使用 关键字:JDBC核心API 内容: 1)加载JDBC驱动: Oracle:Class.forName(“oracle.j ...
- java多线程控制台聊天室(转)
用java多线程实现一个控制台聊天室,呵呵,好玩! 聊天室服务器端 package tf.thread; import java.io.BufferedReader; import java.io.I ...