• POJ 1789 Truck History


    题意:给n个字符串,每个字符串从第一个字符串延伸出来,延伸的代价为两个字符串不同字母的个数,求最大的1/总代价。

    解法:意思就是求最小的总代价……把字符串看做点,字符串之间的代价看做边,形成一个完全图,跑一下prim,因为边数太多了kruskal太慢。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #include<iomanip>
    #define LL long long
    #define lson l, m, rt << 1
    #define rson m + 1, r, rt << 1 | 1
    
    using namespace std;
    string s[2005];
    bool vis[2005];
    struct node
    {
        int u, step;
        node(int u, int step) : u(u), step(step) {}
        node() {}
        bool operator < (const node &tmp) const
        {
            return step > tmp.step;
        }
    };
    int main()
    {
        ios :: sync_with_stdio(false);
        int n;
        while(cin >> n && n)
        {
            memset(vis, 0, sizeof vis);
            for(int i = 0; i < n; i++) cin >> s[i];
            priority_queue<node> q;
            q.push(node(0, 0));
            int ans = 0;
            int cnt = 0;
            while(!q.empty())
            {
                node tmp = q.top();
                q.pop();
                if(vis[tmp.u] == false)
                {
                    cnt++;
                    vis[tmp.u] = true;
                    ans += tmp.step;
                }
                else continue;
                if(cnt == n) break;
                for(int i = 0; i < n; i++)
                {
                    if(vis[i]) continue;
                    int val = 0;
                    for(int j = 0; j < 7; j++)
                        if(s[tmp.u][j] != s[i][j]) val++;
                    q.push(node(i, val));
                }
            }
            cout << "The highest possible quality is 1/" << ans << ".
    ";
        }
        return 0;
    }
    

      

  • 相关阅读:
    ole辅助类sqlhelperaccess
    Asp.net中常用的26个性能优化方法
    MVP模式的相关知识
    ASP.NET AJAX入门系列
    非常实用]Asp.net常用的51个代码
    一步一步学Silverlight 系列文章
    .NET设计模式系列文章
    Asp.net添加上传进度条
    asp.net 用ajax实现文件上传
    增加弹出层的拖拽功能
  • 原文地址:https://www.cnblogs.com/Apro/p/4929905.html
Copyright © 2020-2023  润新知