【POJ 1789】Truck History(最小生成树)
题意:距离定义为两个字符串的不同字符的位置个数。然后求出最小生成树。
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=;
const int M=;
char code[N][];
int f[N];//并查集
struct edge{
int u,v,w;
}e[M];
int n,tot;
void add(int u,int v,int w){
e[tot].u=u;e[tot].v=v;e[tot++].w=w;
}
bool cmp(edge a,edge b){
return a.w<b.w;
}
int find(int x){
if(f[x]==-)return x;
return f[x]=find(f[x]);
}
int Kruskal(){
memset(f,-,sizeof f);
sort(e,e+tot,cmp);
int cnt=,ans=;
for(int i=;i<tot;i++){
int u=e[i].u,v=e[i].v,w=e[i].w;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans+=w;
f[fu]=fv;
cnt++;
}
if(cnt==n-)break;
}
return ans;
}
void solve(){
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
{
int dis=;
for(int k=;k<;k++)
if(code[i][k]!=code[j][k])dis++;
add(i,j,dis);
}
}
int main(){
while(scanf("%d ",&n),n){
tot=;
for(int i = ; i <= n; i++)
gets(code[i]);
solve();
printf("The highest possible quality is 1/%d.\n", Kruskal());
}
return ;
}
【POJ 1789】Truck History(最小生成树)的更多相关文章
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
- 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 (最小生成树)
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 (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- POJ 1789 Truck History (Kruskal 最小生成树)
题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...
随机推荐
- HTML 学习笔记 CSS(轮廓)
轮廓(outline)是绘制于元素周围的一条线 位于边框边缘的外围 可起到突出元素的作用 CSS outline属性规定元素轮廓的样式 颜色和宽度 话不多说 来几个例子 看一看 1:在元素周围画线 & ...
- jquery noConflict详解
noConflict是防止其他库也用了$作为全局变量而引起的冲突,我们看看jquery是怎么做的 首先jquery在代码的开始部分定义了2个私有变量: _jQuery = window.jQuery ...
- xhprof使用笔记(非原创)
[作用] xhprof是facebook开源的一个php性能分析工具. [安装] xhprof扩展的安装: wget http://pecl.php.net/get/xhprof-0.9.2.tg ...
- 启动Oracle
[oracle@redhat ~]$ su - oracle --“切换到oracle用户”Password:[oracle@redha ...
- win10显示此电脑
http://jingyan.baidu.com/article/3aed632e00dfe17011809169.html
- Java中HashSet,HashMap和HashTable的区别
HashMap.HashSet.HashTable之间的区别是Java程序员的一个常见面试题目,在此仅以此博客记录,并深入源代码进行分析: 在分析之前,先将其区别列于下面 1:HashSet底层采用的 ...
- BZOJ 1014 【JSOI2008】 火星人prefix
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- canvas中的碰撞检测笔记
用 canvas 做小游戏或者特效,碰撞检测是少不了的.本文将会涉及普通的碰撞检测,以及像素级的碰撞检测.(本文的碰撞检测均以矩形为例) 普通碰撞检测 普通的矩形碰撞检测比较简单.即已知两个矩形的各顶 ...
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 10(排行榜界面&界面管理)
继上一次介绍了<神奇的六边形>的完整游戏开发流程后(可点击这里查看),这次将为大家介绍另外一款魔性游戏<跳跃的方块>的完整开发流程. (点击图片可进入游戏体验) 因内容太多,为 ...
- Windows Phone 8 显示当前项目的使用内存,最大峰值,最大内存上限
public static class MemoryDiagnosticsHelper { public static bool isStart = false; static Popup popup ...