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 ...
随机推荐
- android怎样查看当前project哪些profile是打开的
代码project里面有三仅仅文件都是涉及到各个profile的宏的,各自是:featureoption.java.common/ProjectConfig.mk.product/ProjectCon ...
- FreeSwitch安装和配置记录
安装FreeSwitch 主要示例,下面的命令: git clone -b v1.2.stable git://git.freeswitch.org/freeswitch.git cd freeswi ...
- hdu 1224 Free DIY Tour(最长的公路/dp)
http://acm.hdu.edu.cn/showproblem.php? pid=1224 基础的求最长路以及记录路径. 感觉dijstra不及spfa好用,wa了两次. #include < ...
- DJ_Java_Decompiler新手入门教程
首先声明:这篇文章并不是我原创,只是感觉挺有用处,想跟大家分享一下,所以标注为原创,希望能有更多的朋友可以看到,还请原作者谅解. 昨天大D说让我写下DJ入门的基础,今天写了一大半了,结果不小心把浏览器 ...
- 如果在线显示php源代码
原文:如果在线显示php源代码 通过php提供的函数highlight_file和highlight_string实现
- 如何高效地向Redis插入大量的数据(转)
最近有个哥们在群里问,有一个日志,里面存的是IP地址(一行一个),如何将这些IP快速导入到Redis中. 我刚开始的建议是Shell+redis客户端. 今天,查看Redis官档,发现文档的首页部分( ...
- poj2226(最小点覆盖)
传送门:Muddy Fields 题意:一个由r行c列方格组成的田地,里面有若干个方格充满泥泞,其余方格都是草.要用长度不限,宽度为1的长木板来覆盖这些泥方格,但不能覆盖草地.最少要用多少个长木板. ...
- android编译自己 内置的jar做法
1.首先 android.mk LOCAL_PATH := $(call my-dir) # ===================================================== ...
- 在ASP.NET MVC 中获取当前URL、controller、action(转)
URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟目录 ...
- veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响
veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响 续接veridata实验系列 上篇:"veridata实验举例(5)改动主键上的列值.update ...