POJ1789 Truck History 【最小生成树Prim】
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 18981 | Accepted: 7321 |
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.
Source
读了两遍题,愣是没读懂,看了讨论区的解释才明确。
题意:给定n个点的编号,用7位长度的字符串表示,随意两个点的距离为它们字符串相应位中不同的字符个数,求MST。
#include <stdio.h>
#include <string.h> #define maxn 2010
#define maxm maxn * maxn
#define inf 0x3f3f3f3f int head[maxn], n, id;
struct Node {
int v, w, next;
} E[maxm];
char map[maxn][8];
int dis[maxn];
bool vis[maxn]; int calDis(int x, int y) {
int sum = 0;
for(int i = 0; i < 7; ++i)
if(map[x][i] != map[y][i]) ++sum;
return sum;
} void addEdge(int u, int v, int w) {
E[id].v = v; E[id].w = w;
E[id].next = head[u]; head[u] = id++;
} void getMap() {
memset(head, -1, sizeof(int) * n);
int i, j, w; id = 0;
for(i = 0; i < n; ++i) {
scanf("%s", map[i]);
for(j = 0; j < i; ++j) {
w = calDis(i, j);
addEdge(i, j, w);
addEdge(j, i, w);
}
}
} int getNext() {
int i, pos = -1, w = inf;
for(i = 0; i < n; ++i)
if(!vis[i] && w > dis[i]) {
w = dis[i]; pos = i;
}
return pos;
} int Prim() {
int i, u, v, sum = 0;
for(i = 0; i < n; ++i) {
vis[i] = 0; dis[i] = inf;
}
dis[u = 0] = 0;
while(u != -1) {
vis[u] = 1; sum += dis[u];
for(i = head[u]; i != -1; i = E[i].next)
if(!vis[v = E[i].v] && dis[v] > E[i].w)
dis[v] = E[i].w;
u = getNext();
}
return sum;
} void solve() {
printf("The highest possible quality is 1/%d.\n", Prim());
} int main() {
while(scanf("%d", &n), n) {
getMap();
solve();
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
POJ1789 Truck History 【最小生成树Prim】的更多相关文章
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- poj1789 Truck History最小生成树
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...
- POJ1789:Truck History(Prim算法)
http://poj.org/problem?id=1789 Description Advanced Cargo Movement, Ltd. uses trucks of different ty ...
- POJ 1789:Truck History(prim&&最小生成树)
id=1789">Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17610 ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- 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: ...
- POJ1789 Truck History 2017-04-13 12:02 33人阅读 评论(0) 收藏
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27335 Accepted: 10634 D ...
- POJ1789 Truck History(prim)
题目链接. 分析: 最大的敌人果然不是别人,就是她(英语). 每种代表车型的串,他们的distance就是串中不同字符的个数,要求算出所有串的distance's 最小 sum. AC代码如下: #i ...
随机推荐
- (step 8.2.8)hdu 1079(Calendar Game)
题目大意是: 两个家伙在区域赛前夕闲的无聊,然后玩一种无限纠结的游戏,随即给定一个日期,每次只能移动day OR month.......... 而且如果下一个月没有当前day的话, 你就不能移动mo ...
- hdu1104 Remainder bfs找算式是否有解……
须要注意的是,进行模运算剪枝-- #include<iostream> #include<queue> #include<cstdlib> #include< ...
- URAL 1707. Hypnotoad's Secret(树阵)
URAL 1707. Hypnotoad's Secret space=1&num=1707" target="_blank" style="" ...
- QTableWidget表格合并若干问题及解决方法
Qt提供 QTableWidget作为表格的类以实现表格的基本功能,表格中所装载的每一个单元格由类QTableWidgetItem提供.这是基于表格实现 Qt提供的一个基础类,若想实现定制表格和单元格 ...
- .bash_profile与.bashrc和.profile的区分概念
在Linux系统中配置环境变量相关的文件主要有如下几个,很容易弄混的,这儿简单区分下: /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/p ...
- HBASE完全分布式模式的安装
1集群环境下hadoop.1.1.2已经安装成功 2配置hosts,及环境变量 3编辑hbase-env.xml 4编辑hbase-site.xml 5编辑regionservers文件 6把Hbas ...
- 浅析——SCTP协议(转)
SCTP处于SCTP用户应用层与IP网络层之间,它运用“关联”(association)这个术语定义交换信息的两个对等SCTP用户间的协议状态 .SCTP也是面向连接的,但在概念上,SCTP“关联”比 ...
- Python金融应用编程(数据分析、定价与量化投资)
近年来,金融领域的量化分析越来越受到理论界与实务界的重视,量化分析的技术也取得了较大的进展,成为备受关注的一个热点领域.所谓金融量化,就是将金融分析理论与计算机编程技术相结合,更为有效的利用现代计算技 ...
- hdu 4714 Tree2cycle dp
用树形dp做的,dp[t][i]表示t及其孩子入度都已经小于等于2并且t这个节点的入度等于i的最优解. 那么转移什么的自己想想就能明白了. 关键在于这个题目会暴栈,所以我用了一次bfs搜索出节点的顺序 ...
- POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...