POJ 1789 Truck History (Kruskal 最小生成树)
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 t o is the original type and t d the type derived from it and d(t o,t d) 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. 意思是给出n个长度相同的字符串,一个字符串代表一个点,每两个字符串有多少个字符不同,则不同的个数即为两点之间的距离,要求各个点都连通求quality的最大值
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX=;
int fa[MAX];
char s[MAX][];
int n,m;
struct Edge
{
int u,v,w;
}e[MAX*MAX/];
int cmp(Edge a,Edge b)
{
return a.w<b.w;
}
void UF_set()
{
for(int i=;i<MAX;i++)
fa[i]=i;
}
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void Union(int a, int b)
{
int r1=Find(a);
int r2=Find(b);
if(r1!=r2)
fa[r2] = r1;
}
void get_map()
{
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
int cnt = ;
for(int k=;k<;k++)
cnt+=(s[i][k]!=s[j][k]);
e[m].u = i;
e[m].v = j;
e[m++].w = cnt;
}
}
}
int Kruskal()
{
int num=,sum=;
UF_set();
for(int i = ; i < m; i++){
int u = e[i].u;
int v = e[i].v;
if(Find(u) != Find(v)){
Union(u, v);
sum += e[i].w;
num++;
}
if(num>=n-) break;
}
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<n;i++)
scanf("%s",s[i]);
m=;
get_map();
sort(e,e+m,cmp);
printf("The highest possible quality is 1/%d.\n",Kruskal());
}
}
POJ 1789 Truck History (Kruskal 最小生成树)的更多相关文章
- POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- 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 (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- 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(最小生成树)
模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...
- POJ 1789 Truck History【最小生成树模板题Kruscal】
题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
随机推荐
- MySQL准入规范及容量评估
一.数据库设计 1.表结构设计 -表中的自增列(auto_increment属性)推荐使用bigint类型 -首选使用非空的唯一键, 其次选择自增列或发号器 不使用更新频繁的列,尽量不选择字符串列,不 ...
- wx事件处理二
wxPython首先在触发对象中查找匹配事件类型的被绑定的处理器函数,如果找到,刚相应方法被执行.如果没找到,wxPython将检查该事件是否传送到了上一级的容器,如果是,父窗口被检查,如此一级级向上 ...
- react +webpack 配置px2rem
项目背景需要适配ipad 以及手机端,这时候当然要告别刀耕火种时代啦(自己算rem),因为已经有成熟的工具啦,即px2rem(https://www.npmjs.com/package/px2rem) ...
- Pro*C介绍
内嵌SQL 概要 Pro*C语法 SQL 预处理指令 语句标号 宿主变量 基础 指针 结构 数组 指示器变量 数据类型同等化 动态SQL 事务 错误处理 SQLCA WHENEVER语句 Demo程序 ...
- Lua 随机数生成问题
原文链接:http://blog.csdn.net/zhangxaochen/article/details/8095007 Lua 生成随机数需要用到两个函数: math.randomseed(xx ...
- (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
在linux环境下配置项目运行环境时,部署的人员都会分配一下内存,以保证程序正常的运行.其实在开发的时候(window系统),就已经涉及到内存分配了,只是这些参数有默认值,因此一直没有去重视它. 以M ...
- node使用 log4js
log4js //配置日志的输出级别,共ALL<TRACE<DEBUG<INFO<WARN<ERROR<FATAL<MARK<OFF八个级别,defau ...
- js同时获得数组的两个最小值
//数组中找两个最小值,及索引 //例如数组: [2,6,7,4,10,3,5]; 计算得出,min1=2,index1=0,min2=3,index2=5; var min1 = Infinity; ...
- Linq测试/查看工具——LinqPad
LinqPad 官方:http://www.linqpad.net/ 介绍: 即时测试任何C#/ F#/ VB片段或程序LINQ支持--SQL / CE / Azure,Oracle,SQLite和M ...
- react native中使用ScrollableTabView
第一步,下载依赖 npm install react-native-scrollable-tab-view --save 第二步,引入 import ScrollableTabView, { Scro ...