码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-1789-Truck History

时间:2019-01-29 00:34:21      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:cin   sum   double   possible   tps   思路   auth   连接   ret   

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

标签:cin   sum   double   possible   tps   思路   auth   连接   ret   

原文地址:https://www.cnblogs.com/YDDDD/p/10332271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!