zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    今日SGU 5.2
    奇异值分解(SVD)小结
    计蒜客16495 Truefriend(fwt)
    计蒜客16492 building(二分线段树/分块)
    hihocoder 1323 回文字符串(字符串+dp)
    hihocoder 1320 压缩字符串(字符串+dp)
    hdu6121 build a tree(树)
    hdu6103 Kirinriki(trick+字符串)
    hdu6097 Mindis(几何)
    hdu 6057 Kanade's convolution(子集卷积)
  • 原文地址:https://www.cnblogs.com/Apro/p/4929905.html
Copyright © 2011-2022 走看看