【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 ...
随机推荐
- java 27 - 5 反射之 通过反射获取成员方法并使用
类Method:提供关于类或接口上单独某个方法(以及如何访问该方法)的信息. A:获取所有方法 数组 1.getMethods 获取该类包括其父类的公共成员方法 2.getDeclaredMetho ...
- 链剖&LCT总结
在搞LCT之前,我们不妨再看看喜闻乐见的树链剖分. 树链剖分有一道喜闻乐见的例题:NOI2015 软件包管理器 如果你看懂题目了,你就会明白它是叫你维护一个树,这棵树是不会动的,要兹磁子树求和,子树修 ...
- Linux收集
1.rsync快速删除文件 rsync --delete -avH /empty /rmdir 选项说明: –delete-before 接收者在传输之前进行删除操作 –progress 在传输时显示 ...
- python里的del变量无法立刻释放内存的解决办法
最近在python开发的时候,用到了一些很占用内存的操作,导致后续程序执行很慢甚至无法执行.探索了一下,最终解决了这个问题. 截图解释: python变量占用了内存,仅仅通过del变量的方式,只是让这 ...
- shenben语录
1.别让别人的一句话将你击垮! 2.任何人都不能替你做决定,除了你自己! 3.对于省选: 想去就去,文化课什么的都不是问题. 如果不去,二十年后的自己一定会后悔的.
- HTML <map> 设置图热点
需要在一张图片中,设置一个区域为热点就用到了<map> 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. <img src="planet ...
- 自己的兼容IE系列的console.log
console.log.apply 在某些浏览器下无法通过if进行验证,只能通过try catch进行验证: window.zlogs = function(){ try{ window.consol ...
- codevs2693 上学路线(施工)
难度等级:黄金 2693 上学路线(施工) 题目描述 Description 问题描述 你所在的城市街道好像一个棋盘,有a条南北方向的街道和b条东西方向的街道. 南北方向a条街道从西到东依次编号为1到 ...
- C#并发编程经典实例--笔记
一.简介 --并发 同时做多件事情 --多线程 并发的一种形式,它采用多个线程来执行程序. **如非必要,代码里不要出现 "new ...
- Web服务器之iis,apache,tomcat三者之间的比较
IIS-Apache-Tomcat的区别 IIS与Tomcat的区别 IIS是微软公司的Web服务器.主要支持ASP语言环境. Tomcat是Java Servlet 2.2和JavaServer P ...