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

链接:

http://poj.org/problem?id=1789

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2005;
const int INF = 0xfffffff;

int n;
int J[N][N], dist[N];
char s[N][10];
bool vis[N];

int Len(char a[], char b[])
{
    int ans=0;

    for(int i=0; i<7; i++)
    {
       if(a[i]!=b[i])
        ans++;
    }
    return ans;
}

int Prim()
{
    int i, j, ans=0;

    memset(vis, 0, sizeof(vis));
    vis[1]=1;

    dist[1]=0;
    for(i=1; i<=n; i++)
        dist[i]=J[1][i];

    for(i=1; i<n; i++)
    {
        int MIN=INF;
        int index=1;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]<MIN)
            {
                index=j;
                MIN=dist[j];
            }
        }
        vis[index]=1;
        if(MIN==INF)
            break;
        ans += MIN;

        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]>J[index][j] && J[index][j]>0 )
                dist[j]=J[index][j];
        }
    }

    return ans;
}

int main ()
{

    while(scanf("%d", &n), n)
    {
        int i, j;

        memset(s, 0, sizeof(s));
        for(i=1; i<=n; i++)
            scanf("%s", s[i]);

        for(i=1; i<=n; i++)
        for(j=1; j<=i; j++)
        {
            J[i][j]=J[j][i]=Len(s[i], s[j]);
        }

        int ans=Prim();
        printf("The highest possible quality is 1/%d.\n", ans);
    }
    return 0;
}
时间: 2024-08-08 22:07:14

(最小生成树)Truck History --POJ -- 1789的相关文章

Truck History POJ 1789

http://poj.org/problem?id=1789 题意:简单来说,就是给出一个 n 行的字符串, 字符串与字符串间的距离为它们相照应的比较有几个不同的字母, 然后求出 n 行字符串直接或间接相连中最短的距离为多少.(生成树模板) 写最小生成树的专题真的比之前其他专题感觉好多了.(%>_<%)   #include<stdio.h> #include<string.h> #include<math.h> #include<queue>

Truck History - poj 1789 (Prim 算法)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company

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(最小生成树)

题意  有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 解题报告

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

【POJ 1789】Truck History

[POJ 1789]Truck History 对于弱这种英语渣来说 这明明就是个英语阅读题啊!!--虽然已经习惯了..不过这题寒假集训刚做过 在哪来一看还是没读懂--千刀万剐煮了我吧 一个字符串由7个小写字母组成 每个字符串表示一种状态 每个状态只能由另一种状态转换 转换耗时为两个字符串中不同字母的数量(即distance -> [0,7]) 读出来这些就好办了 赤果果的最小生成树,Prim Kruskal都可以 测了测这题Kruskal耗时少--但2000个点的话1999000条边(无向)

poj 1789 Truck History(kruskal算法)

题目链接:http://poj.org/problem?id=1789 思路:把每一行看成一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列写的,但是超时啦,不知道为什么,希望有人可以解答.后面用的数组sort排序然后才AC. code: 数组sort排序AC代码: #include<cstdio> #include<queue> #include<algorithm> #include<io