Truck History(poj 1789)
Description
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
Output
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
Hint
需要用普里母算法这种算法让我很是纠结,努力了一天才勉强搞定,很是伤自尊啊
下面是我同学的代码
#include<iostream>
#include<cstdio>
using namespace std;
int grah[][];
int sum=;
void pim(int n)//用Kruskal会超时
{
int i,j,pos;
int min,v[]= {},d[];
for(i=; i<=n; i++)//
d[i]=grah[][i];
v[]=;
for(i=; i<=n; i++)
{
min=;
for(j=; j<=n; j++)
{
if(!v[j]&&min>d[j])
{
min=d[j];
pos=j;
}
}
sum+=min;
v[pos]=;
for(j=; j<=n; j++)
{
if(!v[j])
{
if(d[j]>grah[pos][j])
d[j]=grah[pos][j];
}
}
}
}
int main()
{
int n,i,j,k,cou;
char s[][];
while(cin>>n&&n)
{
getchar();
for(i=; i<=n; i++)
cin>>s[i];
for(i=; i<=n; i++)
{
for(j=i+; j<=n; j++)
{
cou=;
for(k=; k<; k++)
{
if(s[i][k]!=s[j][k])//如果有一个字母不同就+1
cou++;
}
grah[i][j]=grah[j][i]=cou;//标出权重
}
grah[i][i]=;//自己到自己的距离为0;
}
pim(n);
printf("The highest possible quality is 1/%d.\n",sum);
sum=;
}
return ;
}
Truck History(poj 1789)的更多相关文章
- Truck History(卡车历史)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26547 Accepted: 10300 Description Adv ...
- Truck History(kruskal+prime)
Truck History Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...
- poj 1789 Truck History(kruskal算法)
主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...
- POJ 1789 Truck History(Prim+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- POJ 1789:Truck History(prim&&最小生成树)
id=1789">Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17610 ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- Truck History(最小生成树)
poj——Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27703 Accepted: 10 ...
- Truck History(prime)
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31871 Accepted: 12427 D ...
随机推荐
- 如何将ER图转换成关系模式集
在ER图中,主要是实体类型和联系类型. 1.实体类型的转换 (“——”表示对应关系) 实体类型——关系模式 实体的属性——关系模式的属性 实体标识符——关系模式的键 2.联系的转换 一元联系较简单,三 ...
- poj3348 Cows 凸包+多边形面积 水题
/* poj3348 Cows 凸包+多边形面积 水题 floor向下取整,返回的是double */ #include<stdio.h> #include<math.h> # ...
- (转载)Java 1.7的新特性
网上找了一下,整理了贴出来. 下面是Java 1.7 里新出现的功能的一些用法例子: 对集合的支持 摒弃了 Java 集合接口的实现类,如: ArrayList . HashSet 和 HashMap ...
- Android群英传》读书笔记 (4) 第八章 Activity和Activity调用栈分析 + 第九章 系统信息与安全机制 + 第十章 性能优化
第八章 Activity和Activity调用栈分析 1.Activity生命周期理解生命周期就是两张图:第一张图是回字型的生命周期图第二张图是金字塔型的生命周期图 注意点(1)从stopped状态重 ...
- Java基础知识强化之集合框架笔记15:List集合的特点
1. List集合的特点 List本身也是一个接口,如下: public interface List<E> extends Collection<E> (1)List是有序的 ...
- Android(java)学习笔记213:开源框架post和get方式提交数据(qq登录案例)
1.前面提到Http的get/post方式 . HttpClient方式,实际工作的时候不常用到,因为这些方式编写代码是很麻烦的 2.Android应用会经常使用http协议进行传输,网上会有很完善 ...
- Rouh set 入门知识3(上下近似集,正负域,边界域)
在RS中,引入两个概念:一个是下近似集,另一个是上近似集.下近似集是指当一个集合不能利用有效的等价关系被恰当的分类是时,则可通过另外的集合来达到这个集合的近似.形式上,设X⊆U是任一子集,R是U上的等 ...
- CSS Pseudo-Element Selectors伪对象选择符
一: CSS3将伪对象选择符(Pseudo-Element Selectors)前面的单个冒号(:)修改为双冒号(::)用以区别伪类选择符(Pseudo-Classes Selectors),但以前的 ...
- 认识k_BackingField【转】
事情从Json的序列化和反序列化说起. 在C#2.0的项目中,以前经常使用Json.Net实现序列化和反序列化.后来从c#3.0中开始使用新增的DataContractJsonSerializer进行 ...
- 第一次用Github desktop(mac)提交代码遇到的问题
1.新建代码仓库 2.生成密钥 ssh-keygen -C 'your@email.address' -t rsa 3.到根目录下的.ssh文件夹下找到id_rsa.pub文件,将里面的内容复制到下图 ...