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 ...
随机推荐
- Lucene IndexReader,IndexWriter,IndexSearcher 缓存应用
1.IndexManager类,用于提供IndexReader,IndexWriter,IndexSearcher获取接口 import java.io.File; import java.io.IO ...
- my.cnf已经存在,影响安装--mysql
Found existing config file ./my.cnf on the system. Because this file might be in use, it was not rep ...
- Java设计模式01:设计模式的 分类 和 设计原则
一.总体来说设计模式分为三大类: 创建型模式:对象的创建. 创建对象本身是比较耗时的操作,所以我们这里专门找人来帮我们创建对象,我们根据经验总结出来的设计成熟的思路模式. 结构型模式:对象的组成(结构 ...
- Java——(四)Collection之Set集合TreeSet类
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- TreeSet类 TreeSet是SortedSet接口的实现类,正如SortedSet名字所暗 ...
- C#使用框架,打开新选项卡
C#使用框架,打开新选项卡: --打开函数 function Open(text, url) { if ($("#tabs").tabs('exists', text ...
- jquery生产和开发的区别
今天说一下jquery生产和开发的区别,在我们下载jquery的时候,会有两个下载链接,一个是jquery.min.js .迷你版 (生产),另一个是 jquery.js .开发版 .不知道的人可能就 ...
- jquery-动态设置ul li a链接目标
概述: 先上我的布局图: ul中的每一个li里面包含着一个'a'超链接,可以猜想到,我想没点击ul->li里面每个a,链接到不同的页面,在这里,我用iframe完成. html主要代码为: &l ...
- Two ways to create file using 'doc'.
Here are two ways to create file using 'doc' command: Method i: copy con [file name][enter key] [con ...
- 微信小程序开发之入门篇(熟悉开发工具)
个人的每一篇博文都谈不上有什么技术含量,只是为了帮助不熟悉微信小程序开发的自己及他人提供一下思路.谢谢,下面开始! PS: 因为本人没有小程序的内测资格,所以所有的开发及Demo都是无AppId的,如 ...
- angular.js 字符串
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...