其他分享
首页 > 其他分享> > Poj 1789 Truck History

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;
}

 

标签:1789,map,int,pos,++,2001,Poj,Truck,book
来源: https://www.cnblogs.com/Vetsama/p/12234912.html