poj 1789 Truck History(kruskal算法)

题目链接: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算法)

时间: 2024-12-13 15:29:28

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

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

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19860   Accepted: 7673 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

poj 1789 Truck History 解题报告

题目链接:http://poj.org/problem?id=1789 题目意思:给出 N 行,每行7个字符你,统计所有的 行 与 行 之间的差值(就是相同位置下字母不相同),一个位置不相同就为1,依次累加.问最终的差值最少是多少. 额.....题意我是没看懂啦= =......看懂之后,就转化为最小生成树来做了.这是一个完全图,即每条边与除它之外的所有边都连通.边与边的权值是通过这个差值来算出来的. 1 #include <iostream> 2 #include <cstdio>

ZOJ 2158 &amp;&amp; POJ 1789 Truck History (经典MST)

链接:http://poj.org/problem?id=1789 或  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1158 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for br

Kuskal/Prim POJ 1789 Truck History

题目传送门 1 /* 2 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 3 Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Prim.这是最小生成数的裸题,然而题目有点坑爹:( 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <algorithm> 9 #include

POJ 1789 -- Truck History(Prim)

 POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn = 2000 + 10; 6 const int INF = 1000000; 7 int n;//有几个卡车 8 char str[maxn][10]; 9 int d[ma

poj 1789 Truck History (克鲁斯卡尔)

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20140   Accepted: 7791 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

POJ 1789 Truck History(最小生成树)

题意  有n辆卡车  每辆卡车用7个字符表示  输入n  再输入n行字符  第i行与第j行的两个字符串有多少个对应位置的字符不同  i与j之间的距离就是几  求连接所有卡车的最短长度  题目不是这个意思  这样理解就行了 prim啦啦啦啦 #include<cstdio> #include<cstring> using namespace std; const int N = 2005; int cost[N], dis[N][N], n, ans; void prim() { m

poj 1789 Truck History 最小生成树 prim

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

POJ 1789:Truck History 【Prim】

Truck History Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 123   Accepted Submission(s) : 36 Problem Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are