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 ...
随机推荐
- Linux设备驱动程序 之 kmalloc
原型 kmalloc的原型如下: void *kmalloc(size_t size, gfp_t flags) 第一个参数是要分配的块的大小,第二个参数是分片标志: flags标志 最常用的标志是G ...
- vgg16 感受野计算
code: vgg_16 = [ [3, 1], [3, 1], [2, 2], [3, 1], [3, 1], [2, 2], [3, 1], [3, 1], [3, 1], [2, 2], [3, ...
- ArcGIS超级工具SPTOOLS-MXD操作篇
1.1 MXD批量裁剪 操作视频:https://weibo.com/tv/v/Hy7P6bF7d?fid=1034:4381332084881258 把当前窗口的MXD,按某个图层的某个字段批量裁 ...
- APP_DEBUG改成false上线之后发现:“页面错误!请稍后再试~
TP框架 页面错误!请稍后再试 把APP_DEBUG改成false上线之后发现:"页面错误!请稍后再试-". 问题一般是出在,display() 指定某个具体的模板文件后win ...
- java 测试框架
项目开发过程中使用的单元测试框架有Junit.TestNG以及Mockito,Junit和TestNG使用的比较多,Mockito最近才开始使用. TestNG与JUnit的相同点 1. 使用anno ...
- kotlin之null值安全性
var a: String =null // 编译错误 var a: String? =null // 编译通过 要允许null值, 需要将变量声明为可为null的字符串类型:String? fun ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_06-新增页面-前端-新增页面
新建一个添加的页面 复制page_list页面改改名字 page_add 一个标准的页面 <template> <div> 新增页面 </div> </tem ...
- 微信小程序---查看更多的显示与隐藏
平时工作中经常遇到开始只显示几行文字,点击按钮,可以查看全部文字内容. 简单写了个,代码如下: <view class="item_content f28 gray"> ...
- webdriervAPI(上传文件)
from selenium import webdriver driver = webdriver.Chorme() drvier.implicitly_wait(10) driver.ge ...
- Unity Shader基础:编译指令
UntiyShader中,编译指令分为两种: 1.顶点片元着色器(Vetex & Fragment Shader)使用的编译指令 2.表面着色器(Surface Shader)使用的编译指令 ...