poj 1789 Truck History 最小生成树
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 15235 | Accepted: 5842 |
Description
letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types
were derived, and so on.
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
the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.
Output
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
题目大意:给一些字符串,这些字符串都是由7个字母组成,每个字符串代表一个点,点与点的距离是他们的字母不同的个数,让我们求最短距离和
prime算法邻接矩阵实现的,没用优先级队列,很纯的prim算法,500MS AC代码
#include<stdio.h>
#include<string.h> char strs[2010][10];
int map[2010][2010];
int n;//×ܵãÊý
int getdis(char *str1, char *str2)
{
int i;
int ret = 0;
for(i = 0; str1[i]; i++)
{
if(str1[i] != str2[i])
ret ++;
}
return ret;
}
int prim()
{
bool used[2010] = {0};
int dis[2010];
int ans = 0;
memset(dis, -1, sizeof(dis));
int i;
used[0] = 1;
for(i = 0; i < n; i++)
{
dis[i] = map[0][i];
}
for(i = 1; i < n; i++)
{
int j;
int min = 0x7fffffff, mark;
for(j = 0; j < n; j++)
{
if(dis[j] != -1 && min > dis[j] && !used[j])
{
min = dis[j];
mark = j;
}
}
used[mark] = 1;
ans += min;
for(j = 0; j < n; j++)
{
if(!used[j] && ( dis[j] == -1 || dis[j] > map[mark][j]) )
{
dis[j] = map[mark][j];
}
}
}
return ans;
}
int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d", &n), n != 0)
{
memset(map, 0, sizeof(map));
int i;
for(i = 0; i < n; i++)
{
scanf("%s", strs[i]);
int j;
for(j = 0; j < i; j++)
{
int dis = getdis(strs[i], strs[j]);
map[j][i] = dis;
map[i][j] = dis;
}
}
int ans = prim();
printf("The highest possible quality is 1/%d.\n", ans);
}
return 0;
}
poj 1789 Truck History 最小生成树的更多相关文章
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
- POJ 1789 Truck History【最小生成树简单应用】
链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- 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 ...
- POJ 1789 Truck History (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- POJ 1789 Truck History (Kruskal 最小生成树)
题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...
随机推荐
- Android中用双缓存技术,加载网络图片
最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...
- tagName和nodeName的区别
首先介绍DOM里常见的三种节点类型(总共有12种,如docment):元素节点,属性节点以及文本节点,例如<h2 class="title">head</h2 ...
- C#开发BHO程序(引)
一 C#也可以编写BHO程序 因为eve的官方论坛封掉了签名图功能,所以想自己写个BHO插件来“伪造”签名图.研究了几个C++程序,但是始终对响应WebBrowser内的 HTML事件不得其门.这样就 ...
- Apache Kafka 分布式消息队列中间件安装与配置 转载
bin/zkServer.sh start /home/guym/down/kafka_2.8.0-0.8.0/config/zookeeper.properties& bin/kafka-s ...
- Nginx Google 扩展
安装配置详见: https://github.com/cuber/ngx_http_google_filter_module/blob/master/README.zh-CN.md tenginx详见 ...
- Presto 来自Facebook的开源分布式查询引擎
Presto是一个分布式SQL查询引擎, 它被设计为用来专门进行高速.实时的数据分析.它支持标准的ANSI SQL,包括复杂查询.聚合(aggregation).连接(join)和窗口函数(windo ...
- 结合MongoDB开发LBS应用
然后列举一下需求:1.实时性要高,有频繁的更新和读取2.可按距离排序支持分页3.支持多条件筛选(一个经纬度数据还包含其他属性,比如社交系统的性别.年龄) 方案简单介绍:1.sphinx geo索引支持 ...
- 111个知名Java项目集锦,包括url和描述
转:http://www.cnblogs.com/wangs/p/3282183.html 项目名称 项目描述 ASM Java bytecode manipulation framework A ...
- thinkphp禁止模版标签解析
场景: 页面中某些样式或者js中含有tp定义的模版标签,如果被tp当成模版标签解析,就会解析异常. tp中提供了<literal></literal>标签用于禁止标签内部的代码 ...
- 获得Unix/Linux系统中的IP、MAC地址等信息
获得Unix/Linux系统中的IP.MAC地址等信息 中高级 | 2010-07-13 16:03 | 分类:①C语言. Unix/Linux. 网络编程 ②手册 | 4,471 次阅读 ...