最小生成树,主要是题目比较难懂。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int Maxn=+;
const int maxn=*+;
int Father[Maxn];
struct Edge
{
int from,to,w;
}edge[maxn];
int n,tot;
char s[maxn][]; int Find(int x)
{
if(x!=Father[x]) Father[x]=Find(Father[x]);
return Father[x];
} void init()
{
for(int i=;i<=n;i++) Father[i]=i;
tot=;
} bool cmp(const Edge&a,const Edge&b)
{
return a.w<b.w;
} int main()
{
freopen("in.txt","r",stdin);
while(~scanf("%d",&n)){
if(!n) break;
init();
for(int i=;i<n;i++)
scanf("%s",s[i]); for(int i=;i<n;i++)
for(int j=i+;j<n;j++)
{
edge[tot].from=i;
edge[tot].to=j;
edge[tot].w=;
for(int k=;k<;k++)
if(s[i][k]!=s[j][k])
edge[tot].w++;
tot++;
} sort(edge,edge+tot,cmp); int W=;
for(int i=;i<tot;i++)
{
int Fu=Find(edge[i].from);
int Fv=Find(edge[i].to);
if(Fu!=Fv)
{
Father[Fu]=Fv;
W=W+edge[i].w;
}
}
printf("The highest possible quality is 1/%d.\n",W);}
return ;
}

ZOJ 2158 POJ 1789 Truck History的更多相关文章

  1. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  2. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

  3. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  4. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

  6. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

  7. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  8. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

  9. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

随机推荐

  1. Flex 国际化(中英语言适配)

    原文地址:http://www.cnblogs.com/meteoric_cry/archive/2011/01/13/1934404.html(由于此贴时间久远,已做微调) 1.新建Flex Pro ...

  2. 关于oracle数据库(11)

    事务 事务是最小的工作单元,是对数据库的若干操作,增删查改(要不就都成功,要不就都失败) 在oracle数据库中,事务是默认打开的,其他数据库都需要一条语句来打开事务,默认关闭的 事务的特征 原子性. ...

  3. Python--三元运算与lambda表达式

    三元运算: if 1 ==1: name = 'Tim' else: name = 'SB' 利用三元运算来完成上述4句语句任务: name = 'Tim' if 1==1 else 'SB' lam ...

  4. VMware安装Centos6.8设置ip无法远程连接问题

    今天使用VMware安装Centos6.8minimal版本再设置ip地址的时候遇到了一些麻烦,就是无法ping通Centos操作系统的配置的ip从而无法用Xshell远程连接上. 如何配置请看下面的 ...

  5. lr_save_var() 截取任意字符串长度,以参数形式输出(参数转变量)

    Action() { char * desc=(char *)malloc(10);  //定义指针变量,以变量形式存放截取到的参数值 // char * aa = "jadkshfkasd ...

  6. MySQL设置binlog日志的有效期自动回收

    设置日志保留天数,到期后自动删除 查看当前日志保存天数: show variables like '%expire_logs_days%'; 默认是0,即永不过期. 通过设置全局参数修改: set g ...

  7. DOS下导入导出MySQL备份

    导入: 1. cd d:\mysql\bin #cd 到 mysql 的 bin 目录下 2. mysql -u root -p mysql_db_name < d:\mysql\data\ba ...

  8. LOOPS

    LOOPS 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3853 递推 设dp[i][j]为(i,j)到终点期望的使用魔力值,mp[i][ ...

  9. FZU 1502 Letter Deletion(DP)

    Description You are given two words (each word consists of upper-case English letters). Try to delet ...

  10. PAT 团体程序设计天梯赛-练习集 L1-003. 个位数统计

    给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...