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 ...
随机推荐
- VC中如何获取当前时间(精度达到毫秒级)
标 题: VC中如何获取当前时间(精度达到毫秒级)作 者: 0xFFFFCCCC时 间: 2013-06-24链 接: http://www.cnblogs.com/Y4ng/p/Millisecon ...
- qt鼠标事件总结(坐标,跟踪,点击判断)
1.QMouseEvent中的坐标QMouseEvent中保存了两个坐标,一个是全局坐标,当然另外一个是局部坐标.全局坐标(globalPos())即是桌面屏幕坐标(screen coordinate ...
- 如何使用git创建项目,创建分支
git config -global user.name "Your name" git config -global user.email "you@example.c ...
- zoj2314 经典 无源汇有上下界最大流 并输出可行流
ZOJ Problem Set - 2314 Reactor Cooling Time Limit: 5 Seconds Memory Limit: 32768 KB Specia ...
- 推荐2个小工具 .NET reflector resharper
- thinkphp连接mysql5.5版本数据库
//数据库配置信息 'DB_TYPE' => 'mysqli', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => ' ...
- c语言指针点滴1
#include <stdio.h> #include <stdlib.h> void main() { int *p = NULL;//指针开始最好都初始化为空 if(p = ...
- PHP常见报错解析
{错误类型}: {错误原因} in {错误文件} on {错误行数} 说明了在哪个文件的哪一行中因何种原因出现了何种错误. 常见的错误类型一般有下面几种: Parse error(解析错误)一般都伴随 ...
- StoryBoard 加入一个自定义View
1. 建一个 UIView的子类(MyView.h/MyView.m) 2. 建一个 View类型的XIB 3. 把xib的file‘s owner设为MyView 4. 在.h文件里加上 @prop ...
- 域名地址默认跳转到www(301重定向)
要做这个操作之前,你首先必须肯定要有一个域名..... 然后域名指向了某一个外网主机地址,能正常访问网站 IIS7之后版本的看客继续往下看,IIS7之前的版本,请止步,我没有对之前的版本做过 首先确认 ...