给定N个字符串,某个字符串转为另一个字符串的花费为他们每一位不相同的字符数。 求最小花费Q。

Input

多组输入,以0结束。 保证N不超过2000。

Output

每组输出"The highest possible quality is 1/Q."。

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

由于是完全图,所以用prim

1.strlen或者str.length最好用int强制转化一下,要追求0warning

2.distance函数是里面的,不要再include它了

3.判断相等要敲两个等号.

4.'int' is not a class, struct, or union type如果出了这种bug基本就是命名空间混淆了.


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <bitset>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define ll long long
#define mm0(a) memset(a,0,sizeof(a))
#define mm(a,b) memset(a,b,sizeof(a))
#define each(a,b,c) for(int a=b;a<=c;a++)
#define de(x) cout << #x << " " << (x) <<endl
//#define de(x) cout <<""
#define rush() int T;scanf("%d",&T);each(kase,1,T)
#define scan(a,b) scanf("%d%d",&a,&b)
#define fin(a) scanf("%d",&a)
#include <iostream> using namespace std; //const int maxn = 400+5;
const int maxn = 2e3+5;
const int INF = 0x3f3f3f3f;
inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;} bool vis[maxn];
int lowc[maxn];
int Prim(int cost[][maxn],int n)
{
int ans=0;
memset(vis,false,sizeof(vis));
vis[0]=true;//从0开始的
for(int i=1;i<n;i++)lowc[i]=cost[0][i];
for(int i=1;i<n;i++)
{
int minc=INF;
int p=-1;
for(int j=0;j<n;j++)
{
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
}
//de(minc);
if(minc==-1)return -1;//又把==敲成一个等号了
ans+=minc;
vis[p]=true;
for(int j=0;j<n;j++)
{
if(!vis[j]&&lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
} }
return ans;
}
char str[maxn][8];
int cost[maxn][maxn];
int dist(int i,int j)
{
int ans=0;
for(int k=0;k<7;k++)///这里
{
if(str[i][k]!=str[j][k])
ans++;
}
return ans;
}
/*
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
*/
int main()
{
int m;
while(scanf("%d",&m)!=EOF&&m!=0)
{
//getchar();//吃掉回车
for(int i=0;i<m;i++)
cin>>str[i];
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i==j)
{
cost[i][j]=0;
}
else
cost[i][j]=dist(i,j);
//de(cost[i][j]);
}
} int ans=Prim(cost,m);
//de(ans);
printf("The highest possible quality is 1/%d.\n",ans); }
}

POJ 1789 Prim的更多相关文章

  1. Truck History - poj 1789 (Prim 算法)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Ad ...

  2. Kuskal/Prim POJ 1789 Truck History

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

  3. POJ 1789 -- Truck History(Prim)

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

  4. poj 1789 prime

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

  5. POJ 1789 Truck History (Kruskal)

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

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

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

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

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

  8. POJ 1789 Truck History(Prim+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

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

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

随机推荐

  1. 【零基础】AI神经元解析(含实例代码)

    一.序言 关于“深度学习”大部分文章讲的都云里雾里,直到看到“床长”的系列教程以及<深度学习入门:基于Python的理论与实现>,这里主要是对这两个教程进行个人化的总结,目标是让“0基础” ...

  2. insmod内核模块时提示Failed to find the folder holding the modules怎么办?

    答:笔者通过重新编译内核和根文件系统解决了此问题 (笔者使用的是openwrt系统) 分析: 1. ’Failed to find the folder holding the modules‘这句l ...

  3. 006-log-logback,slf4j+logback

    一.概述 Logback作为流行的log4j项目的继承者.它是由log4j的创始人Ceki Gulcu设计的.它是建立在上十年优质日志系统设计经验之上而产生的产品,即logback,它比所有现有的日志 ...

  4. BUUCTF-writeup

    Reverse RSA 使用openssl模块 rsa -pubin -text -modulus -in pub.key得到n值,在 factordb.com上分解大素数得到p,q值,脚本生成pri ...

  5. [转]德哥的PostgreSQL私房菜 - 史上最屌PG资料合集

    链接地址:https://yq.aliyun.com/articles/59251

  6. unity读取灰度图生成等值线图

    准备灰度图 grayTest.png,放置于Assets下StreamingAssets文件夹中.   在场景中添加RawImage用于显示最后的等值线图.   生成等值线的过程,使用Marching ...

  7. 19 个让 MySQL 效率提高 3 倍的 SQL 优化技巧

    优化成本: 硬件>系统配置>数据库表结构>SQL及索引 优化效果: 硬件<系统配置<数据库表结构<SQL及索引 本文我们就来谈谈 MySQL 中常用的 SQL 优化 ...

  8. 查看创世区块 Genesis Block和channel.tx文件

    将 Block 详细内容导入到 json 文件查看 configtxgen -inspectBlock channel-artifacts/genesis.block > genesis.blo ...

  9. hadoop java VM 参数设置 默认native栈大小设置

    问题总结: 程序栈太小,64位机器的栈大小默认比32位的大,将程序从64放到32中执行则报错,需要修改初始堆栈大小 (.so库中提供两个函数接口,一个里面使用的是尺寸较大的图像,另一个处理的图像很小, ...

  10. 【IDEA】格式化代码技巧汇总

    1.格式化 Java 代码 快捷键:Ctrl+Alt+L 2.格式化 Mapper 文件中的 SQL 关联到数据库,让 IDEA 认识你的 SQL.如何关联?选择右侧的database,添加数据库即可 ...