给定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. 解决oracle服务占用内存过高的问题

    其实这是因为安装Oracle时,为了均衡电脑性能和数据库性能,默认内存大小为物理内存的1/8,自身内存比较大时,oracle所占的内存也会变大.而通常,我们自己的环境并不需要分配那么大的内存来支持Or ...

  2. react native Expo完全基于ScrollView实现的下拉刷新和上拉触底加载

    我直接封装成了一个组件 props参数为 static propTypes = { style:PropTypes.object, // 样式 refreshing:PropTypes.bool.is ...

  3. 为什么使用 Web Services?

    最重要的事情是协同工作 由于所有主要的平台均可通过 Web 浏览器来访问 Web,不同的平台可以借此进行交互.为了让这些平台协同工作,Web 应用程序被开发了出来. Web 应用程序是运行在 Web ...

  4. @Autowired注解与@Resource注解的区别与用法

    Spring不但支持自己定义的@Autowired注解,还支持JSR-250规范定义的几个注解.如:@Resource.@PostConstruct及@PreDestroy 1. @Autowired ...

  5. [Scikit-learn] 1.5 Generalized Linear Models - SGD for Classification

    NB: 因为softmax,NN看上去是分类,其实是拟合(回归),拟合最大似然. 多分类参见:[Scikit-learn] 1.1 Generalized Linear Models - Logist ...

  6. Python简单遍历字典及删除元素的方法

    Python简单遍历字典及删除元素的方法 这篇文章主要介绍了Python简单遍历字典及删除元素的方法,结合实例形式分析了Python遍历字典删除元素的操作方法与相关注意事项,需要的朋友可以参考下 具体 ...

  7. php自定义错误处理函数

    function myErrorHandler($errno, $errstr, $errfile, $errline) { echo "<b>Custom error:< ...

  8. onethink多图上传

    模板处理, 可以参考 checkbox 的. 注:edit 方法的 基本一样,需要先把已有的数据展示出来,绑定双击事件,删除图片 需要先绑定给已经展示出来的图片, uploadPicture 的cal ...

  9. 华为ENSP命令大全

    实验命令___ENSP   一. 生成树STP 注:桥优先级取值越小,则优先级越高,通过配置优先级(开销值cost)可控制根桥选举.当根桥发生故障则会选举新的根桥,当故障恢复根桥重新选举.通过设置端口 ...

  10. 最新 咪咕java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.咪咕等10家互联网公司的校招Offer,因为某些自身原因最终选择了咪咕.6.7月主要是做系统复习.项目复盘.LeetCode ...