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 ...
随机推荐
- C程序设计语言练习题1-7
练习1-7 编写一个打印EOF的值的程序. #include <stdio.h> // 包含标准库的信息. int main() // 定义名为main的函数,它不接受参数值. { // ...
- 深入浅出scanf、getcha、gets、cin函数
转:问题描述一:(分析scanf()和getchar()读取字符) scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些 ...
- codeforces Ilya and Matrix
http://codeforces.com/contest/313/problem/C #include <cstdio> #include <cstring> #includ ...
- PSAM读卡芯片TDA8007BHL开发
WWT:Work Waiting Time ATR:Answer To Reset,复位应答 etu =F/Df 1. PSAM概述和应用 PSAM(PurchaseSecure Access ...
- #include"*.c" 文件问题
一般我们学习C语言的时候,include预编译的一般是.h头文件,虽然来说#include却是可以包含任意扩展名的文件,因为考虑到接口与实现分离的问题,头文件里面一般放函数,变量等声明,大家一般都推荐 ...
- 【剑指offer】面试题36:数组中的逆序对
题目: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 思路: 归并排序的合并过程.主要是考虑合并两个有序序列时,计算逆序 ...
- Java Spring各种依赖注入注解的区别
Spring对于Bean的依赖注入,支持多种注解方式: @Resource javax.annotation JSR250 (Common Annotations for Java) @Inject ...
- 为什么Nginx的性能要比Apache高很多?
为什么Nginx的性能要比Apache高很多? 这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的sele ...
- Android开发小问题——java使用
2013-09-25 导语:离上次写博客有点久了,这次写两个开发中解决的问题吧. 正文: 1.ArrayList<E>使用remove问题: 2.字符串映射到函数运行方法: ==== 1. ...
- Android控件属性大全[整理转载]
控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或falseandroid:layout_ ...