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 ...
随机推荐
- 基于Numpy的神经网络+手写数字识别
基于Numpy的神经网络+手写数字识别 本文代码来自Tariq Rashid所著<Python神经网络编程> 代码分为三个部分,框架如下所示: # neural network class ...
- datatable-bootstrap 基本配置
function doSearch() { if(dtable!=null){ dtable.fnClearTable(0); dtable.fnDraw(); // 重新加载数据 }else{ dt ...
- [SDOI2013]泉
题目描述 作为光荣的济南泉历史研究小组中的一员,铭铭收集了历史上x个不同年份时不同泉区的水流指数,这个指数是一个小于. 2^30的非负整数.第i个年份时六个泉区的泉水流量指数分别为 A(i,l),A( ...
- 贪心 HDOJ 5355 Cake
好的,数据加强了,wa了 题目传送门 /* 题意:1到n分成m组,每组和相等 贪心:先判断明显不符合的情况,否则肯定有解(可能数据弱?).贪心的思路是按照当前的最大值来取 如果最大值大于所需要的数字, ...
- I - Andy's First Dictionary(set+stringstream)
Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy ...
- 关于mybatis的xml文件中使用 >= 或者 <= 号报错的解决方案
当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...
- Sql2008事务日志已满处理
处理方式: USE [master] GO ALTER DATABASE gzl SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE gzl SET ...
- servlet下的request&&response
request的方法 *获取请求方式: request.getMethod(); * 获取ip地址的方法 request.getRemoteAddr(); * 获得用户清气的路 ...
- [ Luogu 4917 ] 天守阁的地板
\(\\\) \(Description\) 定义二元函数\(F(x,y)\)表示,用 \(x\times y\) 的矩形不可旋转的铺成一个任意边长的正方形,所需要的最少的矩形个数. 现在\(T\)组 ...
- CF814C An impassioned circulation of affection
思路: 对于题目中的一个查询(m, c),枚举子区间[l, r](0 <= l <= r < n),若该区间满足其中的非c字符个数x不超过m,则可以将其合法转换为一个长度为r-l+1 ...