POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description
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
Output
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最小生成树) 模板题的更多相关文章
- POJ 1789 Truck History【最小生成树模板题Kruscal】
题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...
- POJ 1789 Truck History (Kruskal 最小生成树)
题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...
- 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(最小生成树)
模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...
- poj 1789 Truck History【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- 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 ...
随机推荐
- red note8 pro谷歌套件
谷歌核心Apps(即Google官方应用“全家桶”),包括YouTube,Google Now,Google Play store,Google Play Games,Google Maps等: 基于 ...
- Dockerfile文件记录(用于后端项目部署)
Dockerfile文件记录(用于后端项目部署) 本教程依据个人理解并经过实际验证为正确,特此记录下来,权当笔记. 注:基于linux操作系统(敏感信息都进行了处理) 此文结合另一篇博客共同构成后端服 ...
- AcWing 243. 一个简单的整数问题2 | 树状数组
传送门 题目描述 给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“C l r d”,表示把 A[l],A[l+1],…,A[r] 都加上 d. 2.“Q l r”,表示询问 ...
- 2017CCPC杭州(ABCDJ)
所有的题目在这里<--- 待补... Problem A. HDU6264:Super-palindrome 题意: 题目定义了一个超级回文串,这个串满足:它的任一奇数长度的串都是回文串. 现在 ...
- redux一些自习时候自己写的的单词
setState:设置状态 render:渲染,挂载 dispatchEvent : 派发事件 dispatch:分发,派遣:库里的一个方法,简而言之相当于一个actions和reducer监听方法更 ...
- Ubuntu1804下安装Gitab
部署gitlab 1.配置仓库源 # vim /etc/apt/sources.listdeb http://mirrors.aliyun.com/ubuntu/ bionic main restri ...
- cogs 293. [NOI 2000] 单词查找树 Trie树字典树
293. [NOI 2000] 单词查找树 ★★☆ 输入文件:trie.in 输出文件:trie.out 简单对比时间限制:1 s 内存限制:128 MB 在进行文法分析的时候,通常需 ...
- 手写vue中v-bind:style效果的自定义指令
自定义指令 什么是自定义指令 以 v- 为前缀,然后加上自己定义好的名字组成的一个指令就是自定义指令.为什么要有自定义指令呢?在有些时候,你仍然需要对普通的DOM元素进行底层的操作,这个时候就可以用到 ...
- 异数OS 织梦师-水桶(三)-- RAM共享存储方案
. 异数OS 织梦师-水桶(三)– RAM共享存储方案 本文来自异数OS社区 github: https://github.com/yds086/HereticOS 异数OS社区QQ群: 652455 ...
- IntelliJ IDEA编辑文件的时候CPU飙高问题的解决
原文地址:https://www.javatang.com/archives/2018/04/26/25582403.html 上篇文章中说明了解决IntelliJ IDEA中文输入法无提示的问题,最 ...