【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 ...
随机推荐
- Android开发书籍推荐:从入门到精通系列学习路线书籍介绍
Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 很多时候我们都会不断收到新手的提问"Android开发的经典入门教材和学习路线?"."Android 开发入 ...
- C语言:结构体与数组
#include <stdio.h> struct book{ ]; ]; int price; }; ] = {,,,,,,,,,}; int main(){ struct book * ...
- C# socket UDPの异步链接
基于C#的UDP异步连接,在本机上通过测试
- ICC的sacn-wise和unit-wise
假设有16个被试,2个session,2个RUN,200个ROI,那么ICC需要对RUN1和RUN2分别算两次(相比而言,paired ttest则是对一个session中的RUN1和RUN2计算) ...
- Parallel.Invoke并行你的代码
Parallel.Invoke并行你的代码 使用Parallel.Invoke并行你的代码 优势和劣势 使用Parallel.Invoke的优势就是使用它执行很多的方法很简单,而不用担心任务或者线程的 ...
- noi题库(noi.openjudge.cn) 1.7编程基础之字符串T31——T35
T31 字符串P型编码 描述 给定一个完全由数字字符('0','1','2',-,'9')构成的字符串str,请写出str的p型编码串.例如:字符串122344111可被描述为"1个1.2个 ...
- HTTP协议详细总结
HTTP超文本传输协议,是WWW上应用的最多的协议.了解和掌握HTTP协议是对程序人员的基本要求. 转载请注明出处 http://www.cnblogs.com/zrtqsk/p/3746891.ht ...
- .html 、.htm 、 .shtml 以及 .shtm 四种扩展名的文件区别
新增了一个分类,叫做 Personals,中文我把它解释成 "个人恶趣味",这里将记载一些对工作无关紧要,但是个人又一时有兴趣了解的东西. 今天要讲的是如题的 4 种扩展文件的区别 ...
- 在项目中代替DevExpress(一)
从Delphi时代开始一直都是DevExpress系列控件的忠实用户,到现在已经有10多个年头了.DevExpress里面的控件基本从头到尾都用过一次,而且也开发过很多基于DevExpress的子控件 ...
- mvc5+ef6+Bootstrap 项目心得--WebGrid
1.mvc5+ef6+Bootstrap 项目心得--创立之初 2.mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理 3.mvc5+ef6+Bootstrap 项目心得--WebG ...