首页 > 其他分享 >POJ 1789 Truck History

POJ 1789 Truck History

时间:2023-04-20 21:34:40浏览次数:44  
标签:1789 int quality Truck POJ truck derived type types


Truck History


Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u


Submit  Status  Practice  POJ 1789


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 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.







#include<stdio.h>
#include<string.h>
#define MAX 9999999
char a[10000][10000];
int map[11000][11000],p[10000];
int num[11000];
int n;
void dijkstra()
{
    int s = 1,i,j;
    memset(p,0,sizeof(p));
    memset(num,MAX,sizeof(num));
    for(i=1;i<=n;i++)
    {
        num[i] = map[s][i];
    }
    p[s] = 1;
    int sum = 0;
    for(i=1;i<n;i++)
    {
        int k,min = MAX;
        for(j=1;j<=n;j++)
        {
            if(p[j]==0 && min>num[j])
            {
                min = num[j];
                k = j;
            }
        }
            if(min == MAX)
            {
                break;
            }
            p[k] = 1;
            sum = sum + min;
            for(j=1;j<=n;j++)
            {
                if(p[j] == 0 && num[j]>map[k][j])
                {
                    num[j] = map[k][j];
                }
            }
    }
    printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
    int m,i,j,k;
    while(scanf("%d",&n)!=EOF)
    {
        if(n == 0)
        {
            break;
        }
        for(i=1;i<=n;i++)
        {
            scanf("%s",&a[i]);
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                int count = 0;
                for(k=0;k<7;k++)
                {
                    if(a[i][k]!=a[j][k])
                    {
                        count++;
                    }
                }
                map[i][j] = count;
            }
        }
       /* for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                printf("%d ",map[i][j]);
            }
            printf("\n");
        }*/
        dijkstra();
    }
    return 0;
}




标签:1789,int,quality,Truck,POJ,truck,derived,type,types
From: https://blog.51cto.com/u_14834528/6210675

相关文章

  • Truck History
    TruckHistoryTimeLimit:2000MS MemoryLimit:65536KTotalSubmissions:21508 Accepted:8361DescriptionAdvancedCargoMovement,Ltd.usestrucksofdifferenttypes.Sometrucksareusedforvegetabledelivery,otherforfurniture,orforbricks.The......
  • Robotruck UVA - 1169
    有n个垃圾,第i个垃圾的坐标为(xi,yi),重量为wi。有一个机器人,要按照编号从小到大的顺序捡起所有垃圾并扔进垃圾桶(垃圾桶在原点(0,0))。机器人可以捡起几个垃圾以后一起扔掉,但任何时候其手中的垃圾总重量不能超过最大载重C。两点间的行走距离为曼哈顿距离(即横坐标之差的绝对值加上纵......
  • POJ--3050 Hopscotch(暴搜)
    记录21:362023-4-16http://poj.org/problem?id=3050reference:《挑战程序设计竞赛(第2版)》第二章练习题索引p135DescriptionThecowsplaythechild'sgameofhopscotchinanon-traditionalway.Insteadofalinearsetofnumberedboxesintowhichtohop,thec......
  • kuangbin专题一 简单搜索 迷宫问题(POJ-3984)
    迷宫问题TimeLimit:1000MS MemoryLimit:65536KDescription定义一个二维数组:intmaze[5][5]={0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,};它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编......
  • kuangbin专题一 简单搜索 罐子(POJ-3414)
    PotsTimeLimit:1000MS MemoryLimit:65536KDescriptionYouaregiventwopots,havingthevolumeofAandBlitersrespectively.Thefollowingoperationscanbeperformed:FILL(i)fillthepoti(1≤i≤2)fromthetap;DROP(i)emptythep......
  • poj2750(线段树+复杂区间合并)
    PottedFlowerPOJ-2750思路:我们将题目简单化,假设我们要求的是序列的最大连续子段和,且可以包括所有数。我们的线段树需要维护这段区间的最大前缀和pre,最大后缀和suf,区间和sum,区间连续最大和mx。那么难点就在于如何由子节点更新父节点。我们可以知道,tr[p].sum=tr[lc].sum......
  • kuangbin专题一 简单搜索 找倍数(POJ-1426)
    FindTheMultipleTimeLimit:1000MS MemoryLimit:10000KDescriptionGivenapositiveintegern,writeaprogramtofindoutanonzeromultiplemofnwhosedecimalrepresentationcontainsonlythedigits0and1.Youmayassumethatnisnotgreaterth......
  • poj2777(线段树)
    CountColorPOJ-2777思路:暴力能过,线段树维护这个区间的颜色,如果是混色则置为1,如果是单一颜色则设为这个颜色,修改就是正常的区间修改,区间查询就要变一下。还有题解是用二进制做得,可以学一下。#define_CRT_SECURE_NO_WARNINGS1#include<algorithm>#include<fstream>#inc......
  • kuangbin专题一 简单搜索 翻转(POJ-3279)
    FliptileTimeLimit:2000MS MemoryLimit:65536KDescriptionFarmerJohnknowsthatanintellectuallysatisfiedcowisahappycowwhowillgivemoremilk.HehasarrangedabrainyactivityforcowsinwhichtheymanipulateanM×Ngrid(1≤M≤15;1≤......
  • poj 2182
    LostCowsPOJ-2182与这题一样BuyTickets-POJ2828-VirtualJudge(csgrandeur.cn)题意:有1~NN个数字,这N个数字的顺序是打乱的,从第二个数字开始给你它的前面有多少个数字比他小思路:输入的数字都要加一,然后我们从后往前遍历,在线段树中如果左子树的sum‘>sum,则进入左子......