poj1789
来源:互联网 发布:织梦html5整站源码 编辑:程序博客网 时间:2024/05/16 12:28
算是很基础的最小生成树了吧,我用kruskal做的,机器转了400多ms,说明写的不是很好,目前尚在入门,慢慢来。
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;struct n1{int s,e,w;};n1 path[2100000];int father[2001];int find(int i){while(father[i]!=i){i=father[i];}return i;}bool cmp(n1 a,n1 b){return a.w<b.w;}int kruskal(int v,int e){int temp1,temp2,i,sum,num_bian;v--;i=sum=num_bian=0;sort(path+1,path+e+1,cmp);while(num_bian!=v){i++;temp1=find(path[i].s);temp2=find(path[i].e);if(temp1!=temp2){sum+=path[i].w;num_bian++;if(temp1>temp2)temp1^=temp2^=temp1^=temp2;father[temp2]=temp1;}}return sum;}int juli(char *a,char *b){int i,sum;sum=0;for(i=0;i<7;i++){if(a[i]!=b[i])sum++;}return sum;}int main(){int i,j,k,n;char kache[2001][8];while(scanf("%d",&n)&&n!=0){for(i=1;i<=n;i++){scanf("%s",kache[i]);father[i]=i;}k=0;for(i=1;i<n;i++)for(j=i+1;j<=n;j++){k++;path[k].s=i;path[k].e=j;path[k].w=juli(kache[i],kache[j]);}printf("The highest possible quality is 1/%d.\n",kruskal(n,k));}}
0 0
- poj1789
- poj1789
- poj1789
- poj1789
- poj1789
- POJ1789
- poj1789
- poj1789
- poj1789
- poj1789
- POJ1789
- POJ1789
- poj1789
- poj1789 Prim
- poj1789--prim
- poj1789(prim)
- prim poj1789
- poj1789 prim
- 星型模型与雪花模型
- 插入迭代器
- VS2008向MFC 对话框 添加托盘图标(显示和消失)
- 第12周-项目4-时间日期类 .
- 条款20:宁以pass-by-reference-to-const替换pass-by-value
- poj1789
- 面试题:大小交换
- POJ 1111 Image Perimeters(DFS)
- 快排 + 二分
- 经验总结06--java计时器
- 关于“转载”文章的声明
- 使用批次优化渲染
- Java Swing界面编程(17)---单行文本输入组件:JTextField
- poj 2049