主题链接:http://poj.org/problem?id=1789

思维:一个一个点,每两行之间不懂得字符个数就看做是权值。然后用kruskal算法计算出最小生成树

我写了两个代码一个是用优先队列写的。可是超时啦,不知道为什么。希望有人能够解答。后面用的数组sort排序然后才AC。

code:

数组sort排序AC代码:

#include<cstdio>
#include<queue>
#include<algorithm>
#include<iostream> using namespace std; struct edge
{
int from;
int to;
int cost;
}; bool cmp(edge e1,edge e2)
{
return e1.cost<e2.cost;
} edge node[2001*2001]; int father[2005];
int nn,n; int find(int x) //做并查集查找
{
if(x!=father[x])
{
father[x]=find(father[x]);
}
return father[x];
} void kruskal()
{
int MST=0;
for(int i=0;i<2005;i++)
{
father[i]=i;
}
for(int i=0;i<nn;i++)
{
int fx=find(node[i].from);
int fy=find(node[i].to);
if(fx!=fy)
{
father[fx]=fy;
MST+=node[i].cost;
}
}
printf("The highest possible quality is 1/%d.\n",MST);
}
int main()
{
char str[2005][10];
int i,j;
while(scanf("%d",&n)==1&&n)
{
nn=0;
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
int sum=0;
for(int kk=0;kk<7;kk++)
{
if(str[i][kk]!=str[j][kk])
{
sum++;
}
}
node[nn].from=i;
node[nn].to=j;
node[nn].cost=sum;
nn++;
}
}
sort(node,node+nn,cmp);
kruskal();
}
return 0;
}



优先队列超时代码

#include<cstdio>
#include<queue>
#include<algorithm>
#include<iostream> using namespace std; struct edge
{
friend bool operator<(edge e1,edge e2)
{
return e1.cost>e2.cost;
}
int from;
int to;
int cost;
}; edge e;
priority_queue<edge> Q;
int father[2005];
int nn,n;
int find(int x)
{
if(x!=father[x])
{
father[x]=find(father[x]);
}
return father[x];
} void kruskal()
{
int MST=0;
for(int i=0;i<2005;i++)
{
father[i]=i;
}
int num=0;
while(!Q.empty()&&num!=nn)
{
edge e=Q.top();
//printf("BBB%d %d %d\n",e.from,e.to,e.cost);
Q.pop();
int fx=find(e.from);
int fy=find(e.to);
if(fx!=fy)
{
father[fx]=fy;
MST+=e.cost;
num++;
}
}
printf("The highest possible quality is 1/%d.\n",MST);
}
int main()
{
char str[2005][10];
int i,j;
while(scanf("%d",&n)==1&&n)
{
nn=0;
while(!Q.empty()) Q.pop();
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
int sum=0;
for(int kk=0;kk<7;kk++)
{
if(str[i][kk]!=str[j][kk])
{
sum++;
}
}
nn++;
e.from=i;
e.to=j;
e.cost=sum;
Q.push(e);
//printf("edge:%d %d %d %d\n",e.from,e.to,e.cost,nn);
}
}
kruskal();
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 1789 Truck History(kruskal算法)的更多相关文章

  1. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

  2. POJ 1789 Truck History (Kruskal 最小生成树)

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

  3. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  4. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  5. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

  6. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  7. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  8. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

  9. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

随机推荐

  1. CentOS6.4 编译安装Python 3.3.2 - CRPER木木

    基础环境: CentOS6.4(预装GCC,或者联网YUM---GCC编译写进这里太臃肿,找机会另外写一篇) Python 3.3.2     下载链接: http://www.python.org/ ...

  2. Swift - 使用表格组件(UITableView)实现单列表

    1,样例说明: (1)列表内容从Controls.plist文件中读取,类型为Array. (2)点击列表项会弹出消息框显示该项信息. (3)按住列表项向左滑动,会出现删除按钮.点击删除即可删除该项. ...

  3. Mac必备软件推荐

    阅读原文http://littlewhite.us/archives/245 随着IOS的流行.Mac电脑也越来越多的进入人们的视野,和iPhone系列一样,苹果的Mac产品线也是软硬件完美结合.有着 ...

  4. [C#基础] 类

    类成员 字段和方法是最重要的类成员类型,字段是数据成员,方法是函数成员 字段 字段是隶属于类的变量 它可以是任何类型,无论是预定义类型还是用户定义类型 和所有变量一样,字段用来保存数据 它们可以被写入 ...

  5. linux下C语言socket网络编程简例

    原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...

  6. 面向接口可扩展框架之“Mvc扩展框架及DI”

    面向接口可扩展框架之“Mvc扩展框架及DI” 标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把 ...

  7. python 学习笔记 10 -- 正則表達式

    零.引言 在<Dive into Python>(深入python)中,第七章介绍正則表達式,开篇非常好的引出了正則表達式,以下借用一下:我们都知道python中字符串也有比較简单的方法, ...

  8. linux下动态连接变为静态打包,使用statifier_S展翅飞_新浪博客

    linux下动态连接变为静态打包,使用statifier_S展翅飞_新浪博客 linux下动态连接变为静态打包,使用statifier (2013-04-27 14:38:19) 转载▼

  9. Linux 系统文件夹结构

    登录后,在当前的命令窗口中输入命令: <span style="font-size:14px;">ls /</span> 你的终端上会出现例如以下的代码: ...

  10. asp.net中用FileStream类实现下载文件功能,自定义下载路径,像IE下载一样

    方法一: //这个值可以从配置文件读取,或者放个textbox让用户填 string path = "你的路径";FileStream outputStream = new Fil ...