主题链接: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. 2388 Who&#39;s in the Middle(简单排序)

    训练计划的第一个问题,首先从水问题开始:排序的数组,中间数则输出. http://poj.org/problem?id=2388 冒泡排序: #include <iostream> usi ...

  2. innerHTML与appendChild(newnodeText)的区别

    innerHTML和createTextNode都可以把一段内容添加到一个节点中,区别是如果这段内容中有html标签时表现就不同了,在createTextNode中会当作文本处理,不会被浏览器解析,但 ...

  3. 调整Tomcat的并发线程到5000+

    调整Tomcat的并发线程数到5000+ 1. 调整server.xml的配置 先调整maxThreads的数值,在未调整任何参数之前,默认的并发线程可以达到40. 调整此项后可以达到1800左右. ...

  4. 60s 经济学探奇

    理解经济学 什么是经济学.对于学习金融的同学,一定会给你搬出一大堆定义.例证.学派.说经济学是一门研究研究价值的生产.流通.分配.消费的规律的理论. 非常高大上的感觉,可是对于我这样没有什么金融学理论 ...

  5. Android Studio Gradle 添加.so 支持文件

    近期发展Android Wear 关注商品.官员Demo所有gradle 工程. 当然,我也用eclipse配置一个可行的环境. 问题来了,eclipse,android studio 开发 andr ...

  6. poj 3304(直线与线段相交)

    传送门:Segments 题意:线段在一个直线上的摄影相交 求求是否存在一条直线,使所有线段到这条直线的投影至少有一个交点 分析:可以在共同投影处作原直线的垂线,则该垂线与所有线段都相交<==& ...

  7. cocos2d-x开关按钮类CCControlSwitch

    在test项目中的ControlExtensionText\ CCControlSwitchTest目录下面的CCControlSwitchTest.cpp中,通过这个例子,我们也可以制作出不错的开关 ...

  8. Android中&lt;meta-data&gt;的使用

    在AndroidManifest.xml中.<meta-data>元素能够作为子元素,被包括在<activity>.<application> .<servi ...

  9. Invalid embedded descriptor for ".proto".Dependencies passed (Protobufer)解决办法

    前言 之前开发的时候,发现居然出现了Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the ...

  10. 【Linux编程】存储映射I/O

    存储映射I/O使一个磁盘文件与存储空间中的一个缓冲区相映射,对缓冲区的读.写操作就是对文件的读.写操作,从而能够不再使用read.write系统调用. 将文件映射到存储区的函数由mmap完毕,函数原型 ...