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 ...
随机推荐
- GO不支持数组通过函数参数更改,有点不一样
package main import "fmt" func modify(array []int) { array[] = fmt.Println("In modify ...
- leetcode_Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- C++中实现回调机制的几种方式(一共三种方法,另加三种)
(1)Callback方式Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该方法, 比如Windows的窗口消息处理函数就是这种类型. 比如下面的示例代码,我们在Down ...
- bzoj 1192
http://www.lydsy.com/JudgeOnline/problem.php?id=1192 好像学过一个东西: [0..2^(N+1)-1]内的数都的都可以由2^0,2^1,...,2^ ...
- poj3708:函数式化简+高精度进制转换+同余方程组
题目大意 给定一个函数 找出满足条件 等于 k 的最小的x m,k,d已知 其中 m,k 很大需要使用高精度存储 思路: 对 函数f(m)进行化简 ,令t=ceil( log(d,m) ) 可以得 ...
- Java 8 新特性终极版
声明:本文翻译自Java 8 Features Tutorial – The ULTIMATE Guide,翻译过程中发现并发编程网已经有同学翻译过了:Java 8 特性 – 终极手册,我还是坚持自己 ...
- URAL 1658
题目大意:求出T个最小的满足各个位的和为S1,平方和为S2的数.按顺序输出.数的位数大于100或者不存在这样一个数时,输出:No solution. KB 64bit IO Format:%I ...
- c语言函数定义、函数声明、函数调用以及extern跨文件的变量引用
1.如果没有定义,只有声明和调用:编译时会报连接错误.undefined reference to `func_in_a'2.如果没有声明,只有定义和调用:编译时一般会报警告,极少数情况下不会报警告. ...
- pyqt托盘例子
# -*- coding: cp936 -*- #!/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui ...
- web.xml中contextConfigLocation的作用(转)
原文地址:http://blog.csdn.net/zhangliao613/article/details/6289114 原文格式较乱,此处略作整理.内容未变. 在web.xml中使用contex ...