POJ 1789
来源:互联网 发布:linux vi进入编辑模式 编辑:程序博客网 时间:2024/05/19 00:12
最小生成树问题,用Prim法比Kruskal法好一些,Prim没怎么看过,这次边做边学了。脑子一片浆糊,还交错了一次题。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#define maxn 10using namespace std;int ss[2010][2010];char sa[2010][9];int low[2010];int cc[2010];int w=0,tot;int prim(){ int ans=0; memset(cc,0,sizeof(cc)); for(int i=0;i<tot;i++) { low[i]=ss[i][0]; } cc[0]=1; for(int i=1;i<tot;i++) { int min=maxn,k=0; for(int j=1;j<tot;j++) { if(low[j]<min&&cc[j]==0) { min=low[j]; k=j; } } ans+=min; cc[k]=1; for(int j=1;j<tot;j++) if(!cc[j]&&ss[k][j]<low[j]) { low[j]=ss[k][j]; } } return ans;}int main(){ while(scanf("%d",&tot)&&tot) { memset(ss,0,sizeof(ss)); memset(sa,0,sizeof(sa)); memset(low,0,sizeof(low)); for(int i=0;i<tot;i++) scanf("%s",sa[i]); for(int i=0;i<tot;i++) { for(int j=0;j<tot;j++) { if(i!=j) { for(int ii=0;ii<7;ii++) { if(sa[i][ii]!=sa[j][ii]) { w++; } } ss[i][j]=w; ss[j][i]=w; w=0; } } } printf("The highest possible quality is 1/%d.\n",prim()); }}
- 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
- linux下使用expect实现自动化脚本
- 获取存储过程返回值及代码中获取返回值
- asp.net常用代码(2)
- WebSphere使用 Administration Thin Client
- asp.net常用代码(3)
- POJ 1789
- hdu 4490 Mad Veterinarian ( BFS+链表记录路径 )
- iOS Framework: Introducing MKNetworkKit (MKNetworkKit介绍,入门,翻译)
- 开始新的博客
- Ubuntu 12.04 成功编译安装 GCC 4.8.1
- 0812leetcode刷题总结
- 泛型知识
- 重新设计的道道指令
- 领导力8大忌讳