zoukankan      html  css  js  c++  java
  • POJ 1789 Truck History

    最小生成树问题。

    给你一组字母序列,问你最有可能的演变,也就是把全部的序列连通所花费最小。


    每次派生的花费 取决于两个字符串上 不同的字母个数。

    于是两两算出花费,然后Kruskal算最小。


    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    #include<iostream>
    #include<list>
    #include<set>
    #include<cmath>
    #define INF 0x7fffffff
    #define eps 1e-6
    using namespace std;
    int n,m;
    int fa[2001];
    struct lx
    {
        int u,v,len;
    }l[2000001];
    int father(int x)
    {
        if(x!=fa[x])
            fa[x]=father(fa[x]);
        return fa[x];
    }
    bool cmp(lx a,lx b)
    {
        return a.len<b.len;
    }
    char str[2001][8];
    int getlen(char *a,char *b)
    {
        int ans=0;
        for(int i=0;i<7;i++)
            if(a[i]!=b[i])ans++;
        return ans;
    }
    int main()
    {
        while(scanf("%d",&n),n)
        {
            for(int i=1;i<=n;i++)
            {
                fa[i]=i;
                scanf("%s",str[i]);
            }
            int cot=0;
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    l[cot].u=i;
                    l[cot].v=j;
                    l[cot++].len=getlen(str[i],str[j]);
                }
            }
            sort(l,l+cot,cmp);
            int ans=0;
            for(int i=0;i<cot;i++)
            {
                int fu=father(l[i].u);
                int fv=father(l[i].v);
                if(fu==fv)continue;
                fa[fv]=fu;
                ans+=l[i].len;
            }
            printf("The highest possible quality is 1/%d.
    ",ans);
        }
    }
    


  • 相关阅读:
    洛谷P5281 [ZJOI2019] Minimax搜索
    势函数
    Comet OJ [Contest #5] 迫真大游戏
    洛谷P3307 [SDOI2013] 项链
    洛谷P5985 [PA2019] Muzyka pop
    CF1205E Expected Value Again
    review
    CF891E Lust
    线性代数
    洛谷P4607 [SDOI2018] 反回文串
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4048891.html
Copyright © 2011-2022 走看看