Description

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase 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 input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that 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

For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

题意:每两个点之间都有一条带权边,求最小生成树。因为是稠密图, 所以其实是更适合prim的。。但是这题比较松,krusal也过了

一共有n个点, 有n * n / 2 条边 ,(为防止溢出,要写成n / 2 * n),边数写错会RE

因为是无向图,所以注意一下两个for循环的起点,这样边数才是上面的数量,不然万一写成n * n条边又RE了

模板跑一遍,注意这里kruskal返回的是最小生成树的所有边的权值之和

#include<cstdio>
#include<cstring>
#include<algorithm> typedef long long ll;
using namespace std;
const int maxn = 2000 + 100;//最大点数
const int maxm = maxn / 2 * maxn;//最大边数
int F[maxn];//并查集, F[x]储存x的父节点
int tol;
char a[maxn][7]; struct Edge{
int u, v, w;//储存每条边的两个端点以及边的权值
}edge[maxm];
bool cmp(Edge a, Edge b){
return a.w < b.w;
}
void addedge(int u, int v, int w){//加入最小生成树当中
edge[tol].u = u;
edge[tol].v = v;
edge[tol++].w = w;
}
int find(int x){
if(F[x] == -1) return x;
else return F[x] = find(F[x]);
}
int kruskal(int n){//参数为点的数量
memset(F, -1, sizeof(F));//刚开始每一个点都是一个独立的连通图
int cnt = 0;//已经加入到最小生成树中的边
sort(edge, edge + tol, cmp);
int ans = 0;//最小生成树的边的权值之和
for(int i = 0 ; i < tol; i++){
int u = edge[i].u;
int v = edge[i].v;
int w = edge[i].w;
int t1 = find(u), t2 = find(v);
if(t1 != t2){//u和v不在一个连通图中
ans += w;
F[t1] = t2;
cnt++;
}
if(cnt == n-1) break;
}
if(cnt < n-1) return -1;//不连通
return ans;
}
int getw(int i, int j){
int qq = 0;
for(int k = 0; k <7; k++){
if(a[i][k] != a[j][k])qq++;
}
return qq;
}
int main(){
int n; //下面建立最小生成树的来源图,即n ^2 /2条边的图
while(scanf("%d", &n) && n){
for(int i = 1; i <= n; i++){
scanf("%s", a[i]);
}
tol = 0;
for(int i = 1; i <= n-1; i++)
for(int j = i; j <= n; j++)addedge(i, j, getw(i, j));
printf("The highest possible quality is 1/%d.\n", kruskal(n));
}
return 0;
}

因为是多组数据,所以memset要放在while里面,不然会WA

POJ 1789 Truck History (Kruskal最小生成树) 模板题的更多相关文章

  1. POJ 1789 Truck History【最小生成树模板题Kruscal】

    题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...

  2. POJ 1789 Truck History (Kruskal 最小生成树)

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

  3. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  4. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

  5. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  6. poj 1789 Truck History(最小生成树)

    模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...

  7. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

  8. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  9. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

随机推荐

  1. K8S与harbor的集成

    文章编写在有道云笔记,采用MarkDown编写,迁移太麻烦了,具体链接如下: http://note.youdao.com/noteshare?id=a9d344951e1fbb761ef7e4979 ...

  2. 配置本地目录作为yum端

    ---恢复内容开始--- 最近在配置gnome-session中发现需要太多依赖的包,又由于实验室使用的是局域网,没有办法连接网络,所以想着配置本地yum安装.在网上找了一些资料,经过整理,把自己这次 ...

  3. 网课党的双重大福利!Github学生账号和Educative合作,总共2000美金的课程免费半年啦

    如果你在北美计算机求职,各种算法培训课对你肯定不陌生.其中有一门课叫做System Design的课,更是人人皆知. 系统设计传送门: Grokking the System Design Inter ...

  4. Win7计划任务命令

    计划任务命令 schtasks C:\Users\Administrator>schtasks /? SCHTASKS /parameter [arguments] 描述: 允许管理员创建.删除 ...

  5. express框架中使用nodemon自启动服务

    1.安装nodemon //全局安装 npm install -g nodemon //本地安装 npm install nodemon --save 2.修改package.json配置 " ...

  6. 使用Java实现简单的Http服务器

    在Java中可以使用HttpServer类来实现Http服务器,该类位于com.sun.net包下(rt.jar).实现代码如下: 主程序类 package bg.httpserver; import ...

  7. .Net Core Web Api实践(三).net core+Redis+docker实现Session共享

    前言:上篇文章介绍了.net core+Redis+IIS+nginx实现Session共享,本来打算直接说明后续填坑过程,但毕竟好多坑是用docker部署后出现的,原计划简单提一下.net core ...

  8. VueRouter爬坑第四篇-命名路由、编程式导航

    VueRouter系列的文章示例编写时,项目是使用vue-cli脚手架搭建. 项目搭建的步骤和项目目录专门写了一篇文章:点击这里进行传送 后续VueRouter系列的文章的示例编写均基于该项目环境. ...

  9. python中常⽤的excel模块库

    python中常用的excel模块库&安装方法 openpyxl openpyxl是⼀个Python库,用于读取/写⼊Excel 2010 xlsx / xlsm / xltx / xltm⽂ ...

  10. scrapy在存储数据到json文件中时,中文变成为\u开头的字符串的处理方法

    在settings.py文件中添加 FEED_EXPORT_ENCODING = 'utf-8'