主题链接: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. Windowbuilder之swt designer安装与使用(转)

    SWT可视化设计,可以使用Google的WindowBuilder. 在Google Code中,搜索WindowBuilder就可以看到路径. 在Eclipse中   Help--->Inst ...

  2. html中的table在android端显示

    转载请注明出处:http://blog.csdn.net/u012338845/article/details/46773245 開始都是用Html.fromHtml(source).来显示html的 ...

  3. LeetCode——Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  4. 指尖上的电商---(3)Solr全文搜索引擎的配置

    接上篇,Solr的准备工作完毕后,本节主要介绍Solr的安装,事实上Solr不须要安装.直接下载就能够了      1.Solr配置 下载地址 :http://lucene.apache.org/so ...

  5. OGEngine教程:声音载入

    以下介绍声音资源从载入到播放的一个流程 首先,我们将须要的音频文件放到assets文件夹下,OGE中SoundRes和MusicRes为我们封装了非常多经常使用的方法,能够用于载入及播放等经常使用功能 ...

  6. DelphiXE7中创建WebService(服务端+客户端) good

    相关资料:http://www.2ccc.com/news/Html/?1507.html DelphiXE7新建WebService具体操作:1.打开“DelphiXE7”->“File”-& ...

  7. Google 搜索的基本语法

    ★搜索引擎的选择 先简单说一下"搜索引擎的选择". 在咱们天朝,Google 屡屡被 GFW 骚扰,导致百度占了便宜,成为份额最高的搜索引擎.不过今天这篇教程,俺还是继续拿 Goo ...

  8. Note:This element neither has attached source nor attached Javadoc

    在用Eclipse编写程序时,发现把鼠标放到类名上时出现标题的提示 解决方法: 右击项目,选择 properties –> Java Build Path –> Libraries,如图 ...

  9. Ubuntu下实现双屏独立切换

    在编码时,总觉得屏幕大小不够,要是能多个屏多好,可以这样 把你的显示器连接到你的电脑,然后开启一个终端 输入:xrandr 显示如下: LVDS1 connected 1366x768+1024+0 ...

  10. 1.SQL统计某张表的列数。

    select   count(syscolumns.name)    from   syscolumns   ,   sysobjects       where   syscolumns.id    ...