POJ 1789Truck History(pirme)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 22648 | Accepted: 8781 |
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. 题意:每输入一个字符串代表一个节点,而节点之间的距离就是不同字符串字对应位置不同的字符个数,然后求最小生成树
不同题意的时候完全不知道怎么做,当知道是距离是什么和是求最小生成树的时候就成了水题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int INF = ;
int n;
char str[ + ][];
int edge[ + ][ + ],dist[ + ],vis[ + ];
int weight(char a[],char b[])
{
int ans = ;
for(int i = ; i < ; i++)
if(a[i] != b[i])
ans ++;
return ans;
}
void prime()
{
memset(vis,,sizeof(vis));
for(int i = ; i <= n; i++)
{
dist[i] = edge[][i];
}
vis[] = ;
dist[] = ;
int sum = ;
for(int i = ; i < n; i++)
{
int minn = INF,pos;
for(int j = ; j <= n; j++)
{
if(vis[j] == && dist[j] < minn)
{
minn = dist[j];
pos = j;
}
}
sum += minn;
vis[pos] = ;
for(int j = ; j <= n; j++)
{
if(dist[j] > edge[pos][j])
dist[j] = edge[pos][j];
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
while(scanf("%d", &n) != EOF)
{
if(n == )
break;
for(int i = ; i <= n; i++)
{
scanf("%s", str[i]);
}
for(int i = ; i < n; i++)
{
for(int j = i + ; j <= n; j++)
{
edge[i][j] = edge[j][i] = weight(str[i],str[j]);
}
}
prime();
}
return ;
}
POJ 1789Truck History(pirme)的更多相关文章
- History(历史)命令用法
如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的用法. 使用 HISTTIMEFORMAT 显示时间 ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- History(历史)命令用法15例
导读 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率,本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFOR ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- [转] Linux History(历史)命令用法 15 例
[From]https://linuxtoy.org/archives/history-command-usage-examples.html 如果你经常使用 Linux 命令行,那么使用 histo ...
- History(历史)命令用法 15 例
如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFORMAT ...
- Backbone源码解析(五):Route和History(路由)模块
今天是四月十二号,距离上次写博已经将近二十天了.一直忙于工作,回家被看书的时间占用了.连续两个礼拜被频繁的足球篮球以及各种体育运动弄的精疲力竭,所以很少抽时间来写技术博客.今天抽出时间把backbon ...
随机推荐
- STL整理
sort 升序排列: iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素. iterator up ...
- 16Spring_AOP编程(AspectJ)_最终通知
- vim常用命令汇总
vim常用命令汇总: http://www.cnblogs.com/softwaretesting/archive/2011/07/12/2104435.html 定位 本行第一个字符 ctrl+$ ...
- 使用eclipse+tomcat搭建本地环境
项目开发工具很多,这里简单介绍下使用eclipse+tomcat如何搭建本地环境. 安装开发工具如下: 1. jdk的安装参考 下载地址:http://pan.baidu.com/s/1sj9rVYX ...
- xcode 出现the file couldn't be opened 怎么解决
右键——show In finder——显示xcode包内容——将有数字的删除——把有用的xcode双击
- openssl_final学习总结
openssl学习总结 openssl知识点总结 openssl实践总结 简介 openssl是一个功能丰富且自包含的开源安全工具箱.它提供的主要功能有: SSL协议实现(包括SSLv2.SSLv3和 ...
- jquery封装常用方法
var git = { //true表示格式正确 checkEmail: function (str) { -]{,})(\S*)/g) == null) { return false; } else ...
- 17.C#类型判断和重载决策(九章9.4)
今天来结束第九章,聊下我们经常忽略,但是编译器会帮我们完成的"类型判断和重载决策",理解编译器如何帮我们完成,相信在写代码时会更明确,避免一些编译出错,排查的问题,让我们开发更给力 ...
- Linq之Expression高级篇(常用表达式类型)
目录 写在前面 系列文章 变量表达式 常量表达式 条件表达式 赋值表达式 二元运算符表达式 一元运算符表达式 循环表达式 块表达式 总结 写在前面 首先回顾一下上篇文章的内容,上篇文章介绍了表达式树的 ...
- jQuery基础之(五)jQuery自定义添加"$"与解决"$"的冲突
1.自定义添加$ 从上面四篇文章我们看到jQuery的强大,但无论如何,jQuery都不可能满足所有用户的需求,而且有一些需求十分小众,也不适合放到整个jQuery框架中,正是因为这一点,jQuery ...