Truck History--poj1789
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21534 | Accepted: 8379 |
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. 这题,我是没看懂,后来看的别人的翻译才自己做的!
>
> 例如有如下4个编号:
>
> aaaaaaa
>
> baaaaaa
>
> abaaaaa
>
> aabaaaa
>
> 显然的,第二,第三和第四编号分别从第一编号衍生出来的代价最小,因为第二,第三和第四编号分别与第一编号只有一个字母是不同的,相应的distance都是1,加起来是3。也就是最小代价为3。
显然,最小生成树!
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int per[],n;
struct node
{
int b,e,w;
}s[];//我开的挺大的,怕不够
bool cmp(node x,node y)
{
return x.w<y.w;
}
void init()
{
for(int i=;i<=n;i++)
per[i]=i;
}
int find(int x)
{
int i=x,j;
while(x!=per[x])
x=per[x];
while(i!=x)//我压缩了路径踩过的,超时了好多次
{
j=per[i];
per[i]=x;
i=j;
}
return x;
}
bool join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
return true;
}
return false;
}
int main()
{
char map[][];
while(scanf("%d",&n),n)
{
getchar();
init();
int i,j;
for(int i=;i<n;i++)
scanf("%s",map[i]);
int k=,sum=,t;
for(i=;i<n;i++)
{
for(j=i+;j<n;j++)
{
int cot=;
for(t=;t<;t++)
{
if(map[i][t]!=map[j][t])
cot++;
}
s[k].b=i;
s[k].e=j;
s[k].w=cot;
k++;
}
}
sort(s,s+k,cmp);
for(i=;i<k;i++)
{
if(join(s[i].b,s[i].e))
sum+=s[i].w;
}
printf("The highest possible quality is 1/%d.\n",sum);
}
return ;
}
欢迎留言。
Truck History--poj1789的更多相关文章
- poj1789 Truck History
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...
- POJ1789 Truck History 【最小生成树Prim】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18981 Accepted: 7321 De ...
- POJ1789 Truck History 2017-04-13 12:02 33人阅读 评论(0) 收藏
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27335 Accepted: 10634 D ...
- poj1789 Truck History最小生成树
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...
- Truck History(prim & mst)
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19772 Accepted: 7633 De ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
- 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 ...
- Truck History(kruskal+prime)
Truck History Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...
随机推荐
- 参数解析argparse模块
argparse,python的一个命令行解析模块 import argparse #创建一个命令行解析器 parser = argparse.ArgumentParser() #增添参数 parse ...
- S3C2440的GPIO
S3C2440一共有A B C D E F G H J 共九组IO口,一共是130个,每组IO口的个数如下图所示, 其中A组IO口只有输出功能,没有输入功能, 关于GPXCON寄存器,这个寄存器用来配 ...
- commons-lang使用
跟java.lang这个包的作用类似,Commons Lang这一组API也是提供一些基础的.通用的操作和处理,如自动生成toString()的结果.自动实现hashCode()和equals()方法 ...
- Visual studio 内存不足的解决方案(out of memory)
编译Visual Studio项目,如果出现"out of memory "的编译错误,可以进行如下操作,加大应用程序可以使用的内存. 请先备份好系统和设置好系统还原点,大体步骤是 ...
- Selenium2Library关键字
对Selenium2Library关键字的整理和翻译.英文不好,仅供参考. 1.Add Cookie [ name | value | path=None | domain=None | secure ...
- My advice to young people - Donald Knuth [video]
http://www.youtube.com/watch?v=75Ju0eM5T2c I took a note of what knuth said in the video. 1. Don't d ...
- Qt error:QtThese QT version are inaccessible
安装完Qt Add-in 打开VS2013的时候出现标题错误. QTDIR 需要设置成Qt安装目录下的vc,这个vc目录下包含include,lib,bin等文件夹.或者是在Qt Option里面设置 ...
- Manage Spring Boot Logs with Elasticsearch, Logstash and Kibana
下载地址:https://www.elastic.co/downloads When time comes to deploy a new project, one often overlooked ...
- df 和du 命令统计磁盘空间不准确
Linux & Unix 中 df 和 du 命令统计磁盘空间数值不一致 经常会使用 df 和 du 分别查看磁盘空闲空间和占用空间,偶尔会发现 df(空闲空间) 会小于磁盘大小减去 du(占 ...
- BZOJ1263: [SCOI2006]整数划分
1263: [SCOI2006]整数划分 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 677 Solved: 332[Submit][Status] ...