【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 ...
随机推荐
- BZOJ 2818: Gcd
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4443 Solved: 1960[Submit][Status][Discuss ...
- C语言:将结构体存放到文件中
#include <stdio.h> #include <stdlib.h> #define MAXTLEN 70 #define MAXALEN 70 #define MAX ...
- XMAPP配置
F:\xmapp\apache\conf\extra httpd-vhosts.conf peizhi##<VirtualHost *:80> ## DocumentRoot " ...
- windows live Writer test
package com.newegg.shopping.util.listener; import javax.servlet.http.HttpSessionAttributeListener; i ...
- java多线程系类:基础篇:04synchronized关键字
概要 本章,会对synchronized关键字进行介绍.涉及到的内容包括:1. synchronized原理2. synchronized基本规则3. synchronized方法 和 synchro ...
- jinja模版
实现不同机器的差异化配置 把apache监听的端口统一改为8080 把配置文件files/httpd.conf 文件做成模版 修改lamp.sl ...
- Linux execve函数簇用法
exec函数簇实现的功能都是用一个新程序替换原来的程序,替换的内容包括堆栈段,代码段,进程控制器PCD,但是原进程的PID保持不变 int execl(const char *path, const ...
- Qt环境搭建(Qt Creator)+Visual Studio
1.http://www.cnblogs.com/ranjiewen/p/5318768.html 简述 经常有人问我编写Qt程序时使用什么IDE,其实这个真的很难回答(各有所长),只能说看个人爱好了 ...
- Python __init__.py 作用详解
__init__.py 文件的作用是将文件夹变为一个Python模块,Python 中的每个模块的包中,都有__init__.py 文件. 通常__init__.py 文件为空,但是我们还可以为它增加 ...
- React-Native运行知乎日报遇到的问题
研究几天RN(React-Native)后,跟着官方的demo做了一下电影图片显示的那个,但是总感觉官方的demo欠缺点什么,所以找来找去找到了RN版的知乎日报,话说知乎日报什么版的都有,不信你们上网 ...