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 5.7 并行复制
一.缘由: 某天看到主从复制延时的告警有点频繁,就想着是不是彻底可以解决一下. 一般主从复制,有三个线程参与,都是单线程:Binlog Dump(主) ----->IO Thread (从) - ...
- abap method中的异常处理
1: 抛出异常 2: 处理异常
- map+case结构使用技巧
people.txt文本如下 lyzx1, lyzx2, lyzx3, lyzx4, lyzx5, lyzx6, lyzx7, lyzx7,,哈哈 托塔天王 import org.apache.spa ...
- [django]cbv方式
cbv的方式 1.简单的url from django.views.generic import TemplateView path('', TemplateView.as_view(template ...
- mosquitto SSL认证
[11]MQTT mosquitto 双向SSL认证配置方式 [12]MQTT mosquitto 单向SSL认证的配置方式 Mosquitto服务器的搭建以及SSL/TLS安全通信配置(比较可信) ...
- 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】
1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...
- 隐藏apache服务器信息
安装完apache一般第一时间都是关闭apache的版本信息,黑客会通过apache暴露出来的信息针对性的入侵,为了服务器的安全这些信息一定要及时关闭. 1.隐藏PHP版本 修改php.ini exp ...
- unicode gbk utf-8的差异
GB2312(1980年)定义,包含6763个汉字,682个字符 GBK1.0 定义了21003个汉字,21886个字符 ASCII->GB2312->GBK 编码方式向后兼容,即同一个字 ...
- Jmeter安装与配置
Jmeter下载与安装配置 1.下载地址:https://jmeter.apache.org/ Apache Jmeter首页,点击 Download Releases 然后,选择,安装版本,有li ...
- MyBatis基础入门《二》Select查询
MyBatis基础入门<二>Select查询 使用MySQL数据库,创建表: SET NAMES utf8mb4; ; -- ---------------------------- -- ...