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 ...
随机推荐
- WebResource-asp.net自定义控件引用外部资源方法
rom:http://www.lmwlove.com/ac/ID879 在asp.net中开发自定义控件时,如果我们要用到图片,外部css,js等文件,那么最好的方式就是将这些文件作为自定义控件嵌入的 ...
- 关于第一个Java应用
一.创建Java源文件 Java应用由一个或多个扩展名为".java"的文件构成,这些文件被称为Java源文件,从编译的角度,则被称为编译单元(Compilation Unit). ...
- 如何设置word里的代码格式,使之有底纹的效果
1.测试平台:word2013 2.步骤:设计->底纹->填充->应用于->确定 3.效果:
- Linux 进程与线程三(线程比较--创建线程参数)
int pthread_equal(pthread_t th1,pthread_t th2); pthread_equal函数比较th1与th2是否为同一线程,由于不可以讲pthread_t数据类型认 ...
- log_bin_trust_function_creators变量解释
在MySQL主从复制机器的master的数据库中创建function,报出如下错误: Error Code: 1418. This function has none of DETERMINISTIC ...
- OAF与XML Publisher集成(转)
原文地址:OAF与XML Publisher集成 有两种方式,一种是用VO与XML Publisher集成,另一种是用PL/SQL与XML Publisher集成 用VO与XML Publisher集 ...
- IOS开发之—— ShareSDK的使用
官方下载ShareSDK iOS:http://sharesdk.cn/ ShareSDK社会化分享 包含“社会化分享组件”“社会化登录组件”“第三方评论和赞”三大模块,并有详尽的数据统计后台,助力移 ...
- 使用iScroll实现上拉或者下拉刷新
上拉或者下拉刷新的需求在移动端是非常常见的需求,大部分情况下,实现这个效果都使用网上现有的解决方案,例如有人使用swiper这个插件, 也有人使用iScroll这个滚动插件.本文的示例是利用iscro ...
- Opencv step by step - 加载视频
刚买了本 "学习Opencv" 这本书,慢慢看起来. 一开始就是加载视频了.当然了,首先你要有个视频 从这里下载了一个: tan@ubuntu:~$ wget http://www ...
- 优化Webstorm
Webstorm这个编辑器还是很强大的,而且版本更新得快,支持最新的typescript,就是性能越来越低. 本文总结了一些优化Webstorm的有效方法,希望对大家有所帮助! 测试环境 Mac OS ...