• ZOJ 2158 POJ 1789 Truck History


    最小生成树,主要是题目比较难懂。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    const int Maxn=2000+10;
    const int maxn=2000*2000+10;
    int Father[Maxn];
    struct Edge
    {
        int from,to,w;
    }edge[maxn];
    int n,tot;
    char s[maxn][10];
    
    int Find(int x)
    {
        if(x!=Father[x]) Father[x]=Find(Father[x]);
        return Father[x];
    }
    
    void init()
    {
        for(int i=0;i<=n;i++) Father[i]=i;
        tot=0;
    }
    
    bool cmp(const Edge&a,const Edge&b)
    {
        return a.w<b.w;
    }
    
    int main()
    {
        freopen("in.txt","r",stdin);
        while(~scanf("%d",&n)){
            if(!n) break;
        init();
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
    
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
            {
                edge[tot].from=i;
                edge[tot].to=j;
                edge[tot].w=0;
                for(int k=0;k<7;k++)
                    if(s[i][k]!=s[j][k])
                        edge[tot].w++;
                tot++;
            }
    
        sort(edge,edge+tot,cmp);
    
        int W=0;
        for(int i=0;i<tot;i++)
        {
            int Fu=Find(edge[i].from);
            int Fv=Find(edge[i].to);
            if(Fu!=Fv)
            {
                Father[Fu]=Fv;
                W=W+edge[i].w;
            }
        }
        printf("The highest possible quality is 1/%d.
    ",W);}
        return 0;
    }
  • 相关阅读:
    Tsql 获取服务器信息
    数据字典生成脚本 【转载】
    c# winform文本框数字,数值校验
    ReentrantLock和AbstractQueuedSynchronizer的分析
    多线程
    前缀和与差分数组
    链表
    堆(优先队列)
    排序算法
    二分查找(递归和非递归)
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4726371.html
Copyright © 2020-2023  润新知