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 ...
随机推荐
- LeetCode之有效括号
class Solution: def isValid(self,s): #第一步,if 判断传入为空的情况 if s == "": ...
- golang 编译或链接 c语言动态、静态库的方法, golang 与 c语言 相互调用
1.golang链接c静态库的方法可以见这个示例: https://github.com/atercattus/go-static-link-example https://github.com/sh ...
- [django]restfulapi请求规范
http://www.ruanyifeng.com/blog/2014/05/restful_api.html 方法及作用: GET(SELECT) :从服务器取出资源(一项或多项). POST(CR ...
- nginx的访问控制
一.基于Basic Auth认证与基于IP的访问控制 一.基于Basic Auth认证 Nginx提供HTTP的Basic Auth功能,配置了Basic Auth之后,需要输入正确的用户名和密码之后 ...
- kfrobotaidlog查找
======查看最新的大于6的数量========== select c.questionid from (select a.questionid, b.clientQuestion from sim ...
- python字符串前带u,r,b的含义
1. https://www.cnblogs.com/yanglang/p/7416889.html 2.https://blog.csdn.net/teavamc/article/details/7 ...
- CentOS6.5安装kafka-2.10-0.8.2(单机)
1.下载 地址:https://kafka.apache.org/downloads 本文中下载版本:kafka_2.10-0.8.2.2.tgz 2.安装 安装目录:/usr/local [root ...
- MySQL数据类型--与MySQL零距离接触2-5字符型
CHAR(5) : 存储abc时,由于长度不够,所以abc后面会补充2个空格,也就是abc CHAR 0-255字节 定长字符串 VARCHAR 0-65535 字节 变长字符串 ENUM('valu ...
- ROSETTA使用技巧随笔--蛋白蛋白对接
先写简略版,以后再详细写. 1. 对输入结构进行预处理(refine) $> relax.default.linuxgccrelease -in:file:s input_files/from_ ...
- idc交叉引用
#查找所有调用MSHookFunction的地方 ea=LocByName("MSHookFunction") if None !=ea: refs = idautils.Code ...