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 ...
随机推荐
- java调用restful webservice(转)
一般来说,大家只会用到GET和POST方法来调用. GET方法的话,参数可以写在url里面. 比如说server的interface用的是@RequestParam或者@PathVariable,在客 ...
- perl学习(10) 字符串处理函数和排序
1.1.index Perl 查找子串第一次在大字符串中出现的地方,返回第一个字符的位置. . . my $stuff = “Howordy world!”; my $where3 = index($ ...
- HDU 2159 二维费用背包问题
一个关于打怪升级的算法问题.. 题意:一个人在玩游戏老是要打怪升级,他愤怒了,现在,还差n经验升级,还有m的耐心度(为零就删游戏不玩了..),有m种怪,有一个最大的杀怪数s(杀超过m只也会删游戏的.. ...
- 用js模拟struts2的多action调用
近期修了几个struts2.1升级到2.3后动态方法调用失效的bug,深有感悟, 原始方法能够參考我之前的博文:struts2.1升级到2.3后动态调用方法问题 可是我那种原始方法有一个局限,就是在s ...
- http权威指南 telnet
对于winXP 1.先启动一个telnet程序连接到TCP服务器中. telnet www.joes-hardware.com 80 2.在连接上的TCP服务器的telnet程序窗口中同时按下 &qu ...
- struts2 <s:iterator/>怎样取得循环的索引
<s:iterator value="list" id="user" status="L"> <s:property va ...
- Android Studio显示行数
Android Studio在打开的文件左側单击鼠标右键,也能像Eclipse一样设置显示代码行数,如图1.可是这边跟Eclipse有一个非常大的差别,Eclipse设置后,其余的相应文件也跟着生效, ...
- C++学习之路—继承与派生(二):派生类的构造函数与析构函数
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 由于基类的构造函数和析构函数是不能被继承的,所以 ...
- SQL server语句练习
相关表: <span style="white-space:pre">create table DEPT ( <span style="white-sp ...
- 基于特定值来推断隐藏显示元素的jQuery插件
jQuery-Visibly是一款小巧简单的jQuery隐藏显示元素插件.该插件依据某个元素的值,例如以下拉框的值.输入框的值等来推断是否显示某个指定的元素. 用于推断的值能够是单个值,或者是多个值, ...