poj_1789 Truck History

Truck History

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 12456

Accepted: 4713

题目链接:http://poj.org/problem?id=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 describingeach type of a truck. The code is simply a string of exactly seven lowercaseletters (each letter on each position has a very special meaning but that isunimportant for this task). At the beginning of company's history, just asingle truck type was used but later other types were derived from it, thenfrom the new types another types were derived, and so on.

Today, ACM is rich enough to pay historians to study its history. One thinghistorians tried to find out is so called derivation plan -- i.e. how the trucktypes were derived. They defined the distance of truck types as the number ofpositions with different letters in truck type codes. They also assumed thateach truck type was derived from exactly one other truck type (except for thefirst truck type which was not derived from any other type). The quality of aderivation 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 tois 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 thecodes of truck types, your program should find the highest possible quality ofa derivation plan.

Input

The input consists of severaltest cases. Each test case begins with a line containing the number of trucktypes, N, 2 <= N <= 2 000. Each of the following N lines of inputcontains one truck type code (a string of seven lowercase letters). You mayassume that the codes uniquely describe the trucks, i.e., no two of these Nlines are the same. The input is terminated with zero at the place of number oftruck types.

Output

For each test case, yourprogram should output the text "The highest possible quality is1/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串七个字符的字符串,让你求出每一行相差字符最少的个数总和

解题思路:

         这个题目可以把它抽象成一张完全无向图,每一行为一个节点,节点与节点之间的权值为行与行之间不同字母的个数,这样抽象出来就是一张完全无向图,然后在用prim算法来求该图的最小生成树即可。

代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#define MAX 2003
#define VALUE 0xfffff
using namespace std;

int g[MAX][MAX];
char gra[MAX][7];
int minCost[MAX];
int visited[MAX];

int prim(int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        visited[i]=0;
        minCost[i]=VALUE;
    }
    minCost[0]=0;
    int res=0;
    while(true)
    {
        int t=-1;
        for(i=0;i<n;i++)
        {
            if(visited[i]==0 && (t==-1 || minCost[i]<minCost[t]))
            {
                t=i;
            }
        }
        if(t==-1)
            break;
        visited[t]=1;
        res+=minCost[t];
        for(i=0;i<n;i++)
        {
            if(minCost[i]>g[i][t] && g[i][t]!=0)
            {
                minCost[i]=g[i][t];
            }
        }
    }
    return res;
}


int main()
{
    int n;
    while(true)
    {
        int i,j,k;        
        scanf("%d",&n);
		getchar();
        if(n==0)
            break;
		memset(g,0,sizeof(g));
        char c;
		//建图
        for(i=0;i<n;i++)
        {
            for(j=0;j<7;j++)
            {//输入字符

                scanf("%c",&c);
                gra[i][j]=c;
                for(k=0;k<i;k++)
                {
                    if(gra[k][j]!=c)
                    {
                        g[k][i]++;
						g[i][k]++;
                    }
                }
            }
			getchar();
        }
		printf("The highest possible quality is 1/%d.\n",prim(n));
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值