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 ...
随机推荐
- android 42 获取图片
资源中获取图片:可以从工程assets文件夹.res/drawble文件夹.sd卡.服务端下载图片. 页面: <LinearLayout xmlns:android="http://s ...
- 简单回顾C++中的字符串
C++中有两种字符串形式,一种是C语言字符数组,一般可以使用 char*指针来操作它:另一种是C++中基于标准库的string类型,这算是更高层次的抽象数据类型. 主要讨论一下string类型,既然是 ...
- css 权威指南笔记(二)元素
替换元素 用来替换元素内容的部分并非有文档内容直接表示. img input 非替换元素 其内容由用户代理(通常是一个浏览器)在元素本身生成的框中显示. 块级元素 块级元素生成一个 元素框,(默认)会 ...
- 关于text-align无法居中的问题
昨天项目,一直出现一个无法居中的问题,最后发现竟然是text-align的问题,才发现自己对text-align的理解还是不够透彻,于是在此再举例分析下. css中的元素一共有三类:块元素.行内块和内 ...
- SQL Server 事务、异常
转自: http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html 事务 在数据库中有时候需要把多个步骤的指令当作一个整 ...
- 【css面试题】三个DIV要求水平对齐,左右两个DIV宽度固定为100px,中间那个DIV充满剩余的宽度(至少2种方法)
这是我在一家公司面试时遇到的问题,当时没有答上来!! 所以看到的小伙伴一定要注意了!! 变化浏览器宽度可看到效果: 左 右 中 然后我们来看看代码: 第一种方法:(浮动) <style type ...
- html 新元素
html5新元素 html5语义元素 <header> 定义了文档或者文档的一部分区域的页眉 <nav> 定义了导航链接的部分 <section> 定义了文档的某个 ...
- WPF 媒体播放器(MediaElement)实例,实现进度和音量控制
WPF 视频音频播放控件MediaElement实现进度控制,音量控制实例 说明: 1.Volume控制音量的大小,double类型,并且实现了属性依赖,可以用来双向绑定:在 0 和 1. 之间的线性 ...
- 使用Java BigDecimal进行精确运算
首先我们先来看如下代码示例: public class Test_1 { public static void main(String[] args) { System.out ...
- 替换 wcf 消息传输中的 命名空间
替换 wcf 消息传输中的 命名空间,http://vanacosmin.ro/Articles/Read/WCFEnvelopeNamespacePrefix