zoukankan      html  css  js  c++  java
  • POJ1789-Truck History

    Truck History

    Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company’s history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.

    Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan – i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as
    1/Σ(to,td)d(to,td)

    where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) is the distance of the types.
    Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.
    Input
    The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.
    Output
    For each test case, your program should output the text “The highest possible quality is 1/Q.”, where 1/Q is the quality of the best derivation plan.
    Sample Input
    4
    aaaaaaa
    baaaaaa
    abaaaaa
    aabaaaa
    0
    Sample Output
    The highest possible quality is 1/3.

    题解: 这个题prime,和Kruskal算法都能过。

    prime算法

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char mapp[2010][10];
    int vis[2010];
    int f[2010];
    int n;
    const int inf=0x3f3f3f3f;
    int fun(int i,int j)
    {
        int t=0;
        for(int k=0;k<7;k++)
        {
            if(mapp[i][k]!=mapp[j][k])
            t++;
        }
        return t;
    }
    
    int prime()
    {
        int sum=0, k,min;
        for(int i=0;i<n;i++)
        f[i]=fun(0,i);
        f[0]=0;
        vis[0]=1;  
      for(int i=0;i<n-1;i++)  
      {  
        min=0x3f3f3f3f;  
        for(int j=0;j<n;j++)  
        {  
          if(!vis[j]&&f[j]<min)  
          {  
            min=f[j];  
            k=j;  
          }  
        }  
        sum+=min;  
        vis[k]=1;     
        for(int j=0;j<n;j++)  
        {  
          if(!vis[j]&&f[j]>fun(k,j))  
            f[j]=fun(k,j); 
        }  
      }  
      return sum;  
    }
    
    int main()
    {
    
        while(scanf("%d",&n)!=EOF&&n)
        {
            for(int i=0;i<n;i++)
                scanf("%s",mapp[i]);
                memset(vis,0,sizeof(vis));
            printf("The highest possible quality is 1/%d.
    ",prime());
    
        }
    }
    

    Kruskal算法

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define MAX 2000*2000
    using namespace std;  
    int pre[MAX];  
    struct node1
    {
        char str[100];
    }a[2001];//输入字符串
    struct node  
    {  
        int u;  
        int v;  
        int distance;  //存储距离
    }p[MAX];  
    bool cmp(node a, node b)  
    {  
        return a.distance < b.distance;  
    }  
    int find(int x)
    {
        if(pre[x]==x) return x;
        return pre[x]=find(pre[x]);
    }
    void init(int n)
    {
        for(int i=0;i<n;i++)
        {
            pre[i]=i;
        }
    }
    int main()  
    {  
        int n;
      //  getchar();
        while(scanf("%d",&n)!=EOF&&n!=0)
        {
            getchar();//消回车
            init(n);
            for(int i=0;i<n;i++)
            {
                gets(a[i].str);
            }
            int k=0;
            for(int i=0;i<n-1;i++)
            {
                for(int j=i;j<n;j++)
                {
                    int num=0;
                    p[k].u=i;
                    p[k].v=j;//记录村庄i  j 
                    for(int s=0;s<7;s++)
                    {
                        if(a[i].str[s]!=a[j].str[s])
                        {
                            num++;
                        }//如果对应位置字符不同,用num++累加距离
                    }
                    p[k++].distance=num;//统计村庄IJ间的距离
                }
            }
            sort(p,p+k,cmp);
            int ans=0;
            for(int i = 0; i < k; i++)  
            {  
                int x = find(p[i].u);  
                int y = find(p[i].v);  
                if(x != y)  
                {  
                    ans = ans + p[i].distance;  
                //    printf("%d    !!!
    ",p[i].distance);
                    pre[y] = x;  
                }  
            }  
            printf("The highest possible quality is 1/%d.
    ",ans);
        }
        return 0;  
    }  
  • 相关阅读:
    团队博客——Sprint计划会议1
    团队博客第一次(团队成员及团队作业说明)
    第一次冲刺-团队开发(第一天)
    cnblog评价以及团队软件的部分改善
    Sprint计划会议1
    团队开发--手机小游戏 需求分析
    团队博客和成员
    第一次冲刺01
    第二次冲刺——第二次总结
    团队开发——Alpha版总结会议
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9220253.html
Copyright © 2011-2022 走看看