POJ 1789 Prim
给定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的更多相关文章
- Truck History - poj 1789 (Prim 算法)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20884 Accepted: 8075 Description Ad ...
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- poj 1789 prime
链接:Truck History - POJ 1789 - Virtual Judge https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...
- POJ 1789 Truck History (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- Poj(1789),最小生成树,Prim
题目链接:http://poj.org/problem?id=1789 还是套路. #include <stdio.h> #include <string.h> #define ...
- POJ 1789:Truck History(prim&&最小生成树)
id=1789">Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17610 ...
- POJ 1789 Truck History(Prim+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
随机推荐
- kotlin中对象表达式
在kotlin中,也有类似的功能,但不是匿名类,而是对象,需要使用object关键字,对象要继承的列需要与object之间的冒号(:)分隔. fun main(arg: Array<String ...
- shared pointer
#include <string>#include <fstream>#include <memory>#include <cstdio> class ...
- 12Flutter页面布局 AspectRatio和Cart卡片组件
/* Flutter AspectRatio.Cart卡片组件: AspectRatio的作用是根据设置调整子元素child的宽高比. AspectRatio首先会在布局限制条件允许的范围内尽可能的扩 ...
- 《图解 HTTP》读书笔记
<图解 HTTP>一书是日本学者上野宣所著,2014 年由于均良先生翻译并在国内出版.因为作者使用十分生动的语言和浅显易懂的案例将 HTTP 协议讲解得深入浅出,所以深受开发者喜爱.现在在 ...
- HBase管理与监控——统计表行数
背景 HBase统计 RowCount 的方法有好几种,并且执行效率差别巨大,以下3种方法效率依次提高. 一.hbase-shell的count命令 这是最简单直接的操作,但是执行效率非常低,适用 ...
- 反向代理远端 单台tomcat 使用域名代理
.环境 nginx 10.1.1.161 公网:123.58.251.166 tomcat 10.1.1.103 .远端tomcat 配置 [root@host---- ~]# netstat -tn ...
- 一个小故事,玩转Python-while循环
无论是传统编程场景还是当下火爆的人工智能应用场景,循环的应用都是必不可少的,上一篇文章中阐述了如何使用for循环来进行编程,这篇文章将会由一个小朋友经常听的故事来讲Python编程中的while循环. ...
- Django -- DateTimeField
默认为时区时间时,需要导入django内置的timezone模块 from django.utils import timezone create_at = models.DateTimeField( ...
- 二分类Logistic回归模型
Logistic回归属于概率型的非线性回归,分为二分类和多分类的回归模型.这里只讲二分类. 对于二分类的Logistic回归,因变量y只有“是.否”两个取值,记为1和0.这种值为0/1的二值品质型变量 ...
- utgard OPC 主要功能简介
度娘还行,尽管不好用,但所有的开发人员不懈努力地写博客,能得到很多东西! 这里向所有未谋面的博主们致敬! 搜了一堆OPC资料,在这里整理一下,用一个封装类来说明utgard的主要接口.使用了java自 ...