Truck History(卡车历史)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 26547 | Accepted: 10300 |
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.
Source
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=;
const int inf=maxn*;
int n;
int a[maxn],map[maxn][maxn];
char ch[maxn][];
bool v[maxn];
int bj(int x,int y){
int ret=;
for(int i=;i<;i++)
if(ch[x][i]!=ch[y][i]) ret++;
return ret;
}
void intn(){
for(int i=;i<=n;i++) scanf("%s",ch[i]);
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
map[j][i]=map[i][j]=bj(i,j);
}
int prim(){
memset(v,,sizeof(v));
int ans=,p,b;
v[]=;
for(int i=;i<=n;i++) a[i]=map[][i];
for(int m=;m<n;m++){
p=inf;
for(int i=;i<=n;i++) if(a[i]<p&&!v[i]){p=a[i];b=i;}
ans+=a[b];v[b]=;
for(int i=;i<=n;i++) a[i]=min(a[i],map[b][i]);
}
return ans;
}
int main(){
while(scanf("%d",&n)){
if(!n) break;
else intn();
printf("The highest possible quality is 1/%d.\n",prim());
}
return ;
}
之前的prim打的丑,poj一直给我TLE(只有TLE),果然poj逼格这么高的地方不适合我这样不会英语,又很娇弱的蒟蒻。
题目来源:POJ
Truck History(卡车历史)的更多相关文章
- poj 1789 Truck History【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- History(历史)命令用法
如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的用法. 使用 HISTTIMEFORMAT 显示时间 ...
- Truck History(prim & mst)
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19772 Accepted: 7633 De ...
- poj1789 Truck History
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 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 ...
- History(历史)命令用法15例
导读 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率,本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFOR ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- Truck History(kruskal+prime)
Truck History Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...
随机推荐
- [Swift通天遁地]一、超级工具-(12)使用Toaster制作简短提示语的吐司窗口
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 爬虫—Selenium使用
Selenium使用 Selenium是一个自动化测试工具,可以驱动浏览器器执行特定的动作,如点击,下拉等.同时还可以获取浏览器当前呈现页面的源代码,可见即可爬. 1.准备 我们使用谷歌Chrome浏 ...
- 二分查找 HDOJ 2141 Can you find it?
题目传送门 /* 题意:给出一个数,问是否有ai + bj + ck == x 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x */ ...
- java IO流 之 字节输出流 OutputString()
Java学习重点之一:OutputStream 字节输出流的使用 FileOutPutStream:子类,写出数据的通道 步骤: 1.获取目标文件 2.创建通道(如果原来没有目标文件,则会自动创建一个 ...
- 扩展Snackbar 使其支持居中显示
https://github.com/nispok/snackbar 默认Snackbar支持底部或者顶部显示,不支持居中显示 查看Snackbar.java的源码可以看到createMarginLa ...
- JNDI链接SQLServer数据库步骤
1.配置context.xml文件 在我们的WebRoot目录下,就是和WEB-INF同级的目录下,新建一个META-INF的目录(假如不存在),在该目录下创建一个context.xml文件,并且在c ...
- 学习一波redis
作为一名合格的java程序员,做web开发的,除了java,mysql,免不了用到内存数据库redis. 身为一名菜鸟,是时候来一波redis从入门到放弃了,哦不,从入门到精通.. 一.安装部署red ...
- setTimeout 0
setTimeout 0 就是把事件放到下一次事件循环时调用,至少要一个时钟之后: ); console.log('this should before setTimeout 0'); var i=0 ...
- list.sort结果是None
错误原因: list.sort()功能是针对列表自己内部进行排序, 不会有返回值, 因此返回为None. 举例说明: In [19]: a=["a","c" ...
- spring思想分析
摘要: EveryBody in the world should learn how to program a computer...because it teaches you how to th ...