POJ-1789-Truck History

Posted ydddd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-1789-Truck History相关的知识,希望对你有一定的参考价值。

链接:https://vjudge.net/problem/POJ-1789#author=0

题意:

给n个卡车,由字符串给出,7个字符,两个卡车距离为同一个位置,字符不想等的数目。

求连接n个卡车的最小距离。

思路:

最小生成树

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXM = 4000000+10;
const int MAXN = 2000+10;

struct Path
{
    int _l,_r;
    double _value;
    bool operator < (const Path & that)const{
        return this->_value < that._value;
    }
}path[MAXM];

string lorry[MAXN];


int Father[MAXN];
int n;

int Get_F(int x)
{
    return Father[x] = (Father[x] == x) ? x : Get_F(Father[x]);
}

void Init()
{
    for (int i = 1;i <= n;i++)
        Father[i] = i;
}

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

int main()
{
    while (cin >> n && n)
    {
        Init();
        for (int i = 1; i <= n; i++)
            cin >> lorry[i];
        int pos = 0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = i+1; j <= n; j++)
            {
                int len = Get_Len(lorry[i], lorry[j]);
                path[++pos]._l = i;
                path[pos]._r = j;
                path[pos]._value = len;
            }
        }
        int sum = 0;
        sort(path + 1, path + 1 + pos);
        for (int i = 1; i <= pos; i++)
        {
            int tl = Get_F(path[i]._l);
            int tr = Get_F(path[i]._r);
            if (tl != tr)
            {
                Father[tl] = tr;
                sum += path[i]._value;
            }
        }
        cout << "The highest possible quality is 1/" << sum << ‘.‘ << endl;
    }

    return 0;
}

  

以上是关于POJ-1789-Truck History的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1789 Truck History

POJ 1789 Truck History

POJ——T1789 Truck History

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

POJ 1789 -- Truck History(Prim)

POJ 1789-Truck History