这题要把给的字符串变成边的权值

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std; #define sf scanf
#define pf printf
#define debug printf("!\n")
#define blank printf("\n")
#define mem(a,b) memset(a,b,sizeof(a)) const int MaxN = ;
const int INF = <<; int p[MaxN]; char str[][]; int w[MaxN*MaxN],r[MaxN*MaxN],u[MaxN*MaxN],v[MaxN*MaxN]; int m,n; int find(int x){return p[x]==x?x:p[x]=find(p[x]);} int cmp(const int a,const int b)
{
return w[a]<w[b];
} int kruskal()
{
int ans = ,i; for(i = ;i<n;i++) p[i] = i;
for(i = ;i<m;i++) r[i] = i; sort(r,r+m,cmp); for(i = ;i<m;i++)
{
int e = r[i];
int x = find(u[e]);
int y = find(v[e]);
if(x!=y)
{
ans+=w[e];p[x] = y;
}
}
return ans;
} int weight(int i,int j)
{
int w = ,k;
for(k = ;k<;k++)
if(str[i][k]!=str[j][k])
w++;
return w;
} int main()
{
int i,j;
while(~sf("%d",&n),n)
{
m = ;
mem(u,);
mem(v,);
mem(w,); for(i = ;i<n;i++)
{
sf("%s",str[i]);
} for(i = ;i<n;i++)
{
for(j=i+;j<n;j++)
{
int tmp = weight(i,j);
if(tmp==)
break;
u[m] = i;
v[m] = j;
w[m++] = tmp;
}
}
int ans = kruskal();
pf("The highest possible quality is 1/%d.\n",ans); } return ;
}

POJ 1789(最小生成树)的更多相关文章

  1. poj 1789 prime

    链接:Truck History - POJ 1789 - Virtual Judge  https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...

  2. POJ 1789 -- Truck History(Prim)

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

  3. POJ 1789 Truck History (Kruskal)

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

  4. Kuskal/Prim POJ 1789 Truck History

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

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

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

  6. poj 1789 Truck History(最小生成树)

    模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...

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

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

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

    链接: http://poj.org/problem?id=1789 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2213 ...

  9. POJ 1789:Truck History(prim&amp;&amp;最小生成树)

    id=1789">Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17610   ...

  10. Poj(1789),最小生成树,Prim

    题目链接:http://poj.org/problem?id=1789 还是套路. #include <stdio.h> #include <string.h> #define ...

随机推荐

  1. CodeForces1142/1143题解

    题面 传送门(1143) 传送门(1142) \(1143A\) 咕咕 n=read(); fp(i,1,n)a[i]=read(),++cnt[a[i]]; fp(i,1,n)if(++c[a[i] ...

  2. 由一个场景分析Mysql的join原理

    背景 这几天同事写报表,sql语句如下 select * from `sail_marketing`.`mk_coupon_log` a left join `cp0`.`coupon` c on c ...

  3. 调用jdbc已经写成的方法----jdbc工具类抽取方式三

    package jdbc_demo3; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.R ...

  4. java获取某段时间内的月份列表

    /**获取两个时间节点之间的月份列表**/ private static List<String> getMonthBetween(String minDate, String maxDa ...

  5. 总博客 wjyyy

    更多文章可见http://www.wjyyy.top/

  6. [转] CentOS7 用 kubeadm 快速安装 Kubernetes v1.13.4 最新教程

    [转 + 编辑][From] https://www.jianshu.com/p/4d61f18bc62d  , https://www.jianshu.com/p/5ff6e26d1912 时间是2 ...

  7. Android 4.4 KitKat终于支持录屏(Screen Recording)了!

    本文介绍了Android 4.4 KitKat系统新增加的录屏功能以及录屏方法,和限制因素.如果App由于版权方面的原因,不想被记录屏幕录像的话,APP只需要在相应的SurfaceView请求“Sur ...

  8. Vagrant 创建虚拟机

    Vagrant  创建虚拟机 1. 下载相关软件 虚拟机软件:vmware  virtualbox Vagrant 软件:vagrant cd /tmpwget http://download.vir ...

  9. linux 和 windows 安装composer

    在 Linux 和 Mac OS X 中可以运行如下命令: curl -sS https://getcomposer.org/installer | phpmv composer.phar /usr/ ...

  10. PHP之string之ord()函数使用

    ord (PHP 4, PHP 5, PHP 7) ord - Return ASCII value of character ord - 返回字符的 ASCII 码值 Description int ...