Poj 1789 Truck History

Posted vetsama

tags:

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

题意就是一堆字符串,每个点之间的距离就是字符串不同的字母个数,字符串长度都是7

把不同字符个数存到图里面,然后用prim算法就行

poj放不下map[2001][2001],所以放到全局区

 

#include<iostream>
#include<string>
using namespace std;

        int map[2001][2001] = { 0 };
int main() {
    int n;
    while (cin >> n) {
        if (n == 0) {
            break;
        }
        string str[2001];
        for (int i = 0; i < n; i++) {
            cin >> str[i];
        }
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int t = 0;
                for (int k = 0; k < 7; k++) {
                    if (str[i][k] != str[j][k])
                        t++;
                }
                map[i][j] = map[j][i] = t;
            }
        }


        int sum = 0;
        int pos[2001] = { 0 };
        int book[2001] = { 0 };
        for (int i = 0; i < n; i++) {
            pos[i] = map[0][i];
        }
        book[0] = 1;

        for (int k = 1; k < n; k++) {
            int min = 99999999;
            int t = 0;
            for (int i = 0; i < n; i++) {
                if (pos[i] < min && book[i] == 0) {
                    min = pos[i];
                    t = i;
                }
            }
            if (t == 0) {
                break;
            }
            sum += min;
            book[t] = 1;
            for (int i = 0; i < n; i++) {
                if (map[t][i] < pos[i] && book[i] == 0) {
                    pos[i] = map[t][i];
                }
            }
        }

        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