您的位置:首页 > 其它

poj_1789 Truck History

2012-08-03 18:09 357 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: