POJ 1789
来源:互联网 发布:虚拟光盘软件下载 编辑:程序博客网 时间:2024/05/19 02:05
最小生成树,题意比较难理解。
建模:
每个字符串可以转化为图上的一点,每两个字符串之间不同的字母的个数可以转化为两点之间的距离,求最小距离即可。
AC代码:
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;#define maxx 2005int n;int esum,p[maxx];struct Edge{ int u,v,w;}edge[maxx*maxx/2];bool cmp(Edge a,Edge b){ return a.w<b.w;}int findpa(int x){ return p[x]==x?x:p[x]=findpa(p[x]);}int kruskal(){ for(int i=1;i<=n;i++) p[i]=i; int ans=0,num=0; sort(edge,edge+esum,cmp); for(int i=0;i<esum;i++) { int p1=findpa(edge[i].u); int p2=findpa(edge[i].v); if(p1!=p2){ ans+=edge[i].w; num++; p[p1]=p2;}if(num==n-1)return ans; } return ans;}int main(){char s[maxx][7];while(scanf("%d",&n)!=EOF&&n){ esum=0; for(int i=1;i<=n;i++) scanf("%s",s[i]); for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++) { int num=0; for(int k=0;k<7;k++){ if(s[i][k]!=s[j][k]) num++;}edge[esum].u=i;edge[esum].v=j;edge[esum].w=num;esum++; } int ans=kruskal(); printf("The highest possible quality is 1/%d.\n",ans);}return 0;}
0 0
- POJ 1789
- POJ 1789
- poj 1789
- poj 1789
- POJ 1789
- poj 1789
- POJ 1789
- POJ 1789
- poj.1789
- POJ 1789
- poj-1789
- poj 1789
- poj 1789
- POJ 1789
- poj 1789
- POJ-1789
- POJ 1789
- POJ 1789
- Windows安装pip
- Write Small Functions Using Examples
- px(像素)与 dp, sp换算公式
- 1009. Product of Polynomials (25)
- error C2248: “CObject::CObject”: 无法访问private 成员(在“CObject”类中声明)
- POJ 1789
- pulltorefresh自动刷新
- Java报表FineReport在医院院长查询分析系统中有什么用
- 来京学习第二天
- Android中的蓝牙知识
- Write Tests for People
- 个人对泛型Class<T>的理解
- Block介绍(四)解开神秘面纱(下)
- static修饰符