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

    Truck History
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 26485   Accepted: 10279

    Description

    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 to is the original type and td the type derived from it and d(to,td) 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.
    

    Source

    CTU Open 2003
    题目大意时间:n个车牌号,刚开始只有一个车牌,其他车牌都是由一个车牌直接或间接产生 一个车牌到另一个车牌的产生权值是它们之间的数字不同的个数 问产生的最小的边权和
     1 #include<cstring>
     2 #include<iostream>
     3 #include<cstdio>
     4 using namespace std;
     5 const int maxn=2010;
     6 const int INF=0x3f;
     7 int map[maxn][maxn],dis[maxn],n;
     8 bool vis[maxn];
     9 char str[maxn][15];
    10 int solve(){
    11     for(int i=1;i<=n;i++) dis[i]=INF;
    12     dis[1]=0;
    13     memset(vis,0,sizeof(vis));
    14     int MST=0;
    15     while(1){
    16         int Mix=INF,pos;
    17         for(int i=1;i<=n;i++)
    18           if(!vis[i]&&dis[i]<Mix)
    19             Mix=dis[ pos=i ];
    20         if(Mix == INF) break;
    21         vis[pos]=true;
    22         MST+=Mix;
    23         for(int j=1;j<=n;j++)
    24           dis[j]=min(dis[j],map[pos][j]);
    25     }
    26     return MST;
    27 }
    28 int main()
    29 {
    30     while(scanf("%d",&n)==1&& n!=0){
    31         for(int i=1;i<=n;i++){
    32             scanf("%s",str[i]+1);
    33             for(int j=1;j<i;j++){
    34                 int sum=0;
    35                 for(int k=1;k<=7;k++)
    36                   if(str[i][k] != str[j][k]) sum++;
    37                 map[i][j]=map[j][i]=sum;
    38             }
    39         }
    40         int ans=solve();
    41         printf("The highest possible quality is 1/%d.
    " , ans);
    42     }
    43     return 0;
    44 }

    思路:见图,跑最小生成树,据说不加优化的Prim算法最优秀

    Kursual版:

    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int maxn=2010;
    const int INF=0x3f;
    int n,tot,fa[maxn];
    struct Edge{
        int from,to,value;
        bool operator < (const Edge &a) const{
            return value<a.value;
        }
    }e[2000500];
    char str[maxn][15];
    int find(int x){
        if(x==fa[x]) return x;
        else return fa[x]=find(fa[x]);
    }
    int main()
    {
        while(scanf("%d",&n)){
            if(n == 0) break;
            int Mst=0;tot=0;
            memset(e,0,sizeof(e));
            for(int i=1;i<=n;i++) scanf("%s",str[i]+1);
            for(int i=1;i<=n;i++) fa[i]=i;
            for(int i=1;i<n;i++){
                for(int j=i+1;j<=n;j++){
                    int sum=0;
                    for(int k=1;k<=7;k++)
                      if(str[i][k] != str[j][k]) sum++;
                    e[++tot].from=i;e[tot].to=j;e[tot].value=sum;
                }
            }
            sort(e+1,e+tot+1);
            int cur=0;
            for(int i=1;i<=tot;i++){
                int u=e[i].from,v=e[i].to,w=e[i].value;
                int rx=find(u),ry=find(v);
                if(rx!=ry){
                    fa[rx]=ry;
                    Mst+=w;cur++;
                }
                if(cur == n-1) break;
            }
            printf("The highest possible quality is 1/%d.
    " ,Mst);
        }
        return 0;
    }

    注:上面标红色的那一句,我刚开始写在定义循环变量j那里,会导致WA掉。。。

  • 相关阅读:
    页面跳转的方式
    OC中的属性特征
    入门模拟——3.2查找元素 B1041 考试座位号 + long long型存储知识
    入门模拟A1009 Product of Polynomials(25)
    入门模拟A1002 A+B for Polynomials(25)
    入门模拟 B1010——一元多项式求导
    A1065——入门模拟 A+B and C
    微信小程序开发——XMXL语法+列表渲染-条件渲染
    微信小程序开发——配置文件夹和导航栏
    微信小程序开发 小程序框架及文件目录介绍
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6369064.html
Copyright © 2011-2022 走看看