ACM: 图论题 poj 1789 (一次AC的水…

来源:互联网 发布:淘宝网店加盟哪家好 编辑:程序博客网 时间:2024/05/18 23:53

                                                                          Truck History

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 ofa truck. The code is simply a string of exactly seven lowercaseletters (each letter on each position has a very special meaningbut that is unimportant for this task). At the beginning ofcompany's history, just a single truck type was used but laterother types were derived from it, then from the new types anothertypes 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 distanceof truck types as the number of positions with different letters intruck type codes. They also assumed that each truck type wasderived from exactly one other truck type (except for the firsttruck type which was not derived from any other type). The qualityof 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 plansuch that to is the original type and td thetype derived from it and d(to,td) is thedistance 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 thehighest possible quality of a derivation plan.

Input

The input consists of several test cases. Each test case beginswith a line containing the number of truck types, N, 2<= N <= 2 000. Each of the followingN lines of input contains one truck type code (a string of sevenlowercase letters). You may assume that the codes uniquely describethe trucks, i.e., no two of these N lines are the same. The inputis terminated with zero at the place of number of truck types.

Output

For each test case, your program should output the text "Thehighest possible quality is 1/Q.", where 1/Q is the quality of thebest derivation plan.

Sample Input

4

aaaaaaa

baaaaaa

abaaaaa

aabaaaa

0

 

Sample Output

The highest possible quality is 1/3.

题意:  卡车的编码 , 每个编码对应一部卡车 , 编码是7为小写字母构成 ,定义两部车的distance
          是的每一位不同的话就distance++ , 现在求:
      1/Σ(to,td)d(to,td)= 1 / sum(t0,td);
    
转化为最小生成树问题.
解题思路:
               1.题意已经公式说明,最小生成树问题,输入是看作一个一个point , prim水之.
               2.计算距离的函数,distance(char* a , char* b);

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int INF = (1<<20);
#define MAX 2005

int dist[MAX];
bool vis[MAX];
int n;
char str[MAX][10];

int distance(char* a,char* b)
{
    int w =0;
    for(int i =0; i < 7; ++i)
    {
      if(a[i] != b[i])
         w++;
    }
    
    returnw;
}

void read_graph()
{
    int i;
    for(i = 1; i<= n; ++i)
    {
      scanf("%s",str[i]);
    }
}

int prim()
{
   memset(vis,0,sizeof(vis));
    int min =INF;
    int i , j ,k;
    int result =0;
    for(i = 1; i<= n; ++i)
    {
      dist[i] = distance(str[1],str[i]);
   //   cout << dist[i]<< endl;
    }
    vis[1] =true;
    
    for(i = 2; i<= n; ++i)
    {
      min = INF;
      for(j = 1; j <= n; ++j)
      {
         if(vis[j] != true&& min >dist[j])
         {
            k = j;
            min = dist[k];
         }
      }
      vis[k] = true;
      result += min;
      
      for(j = 1; j <= n; ++j)
      {
         int tt = distance(str[j],str[k]);
         if(vis[j] != true&& tt <dist[j])
         {
            dist[j] = tt;
         }
      }
    }
    
    returnresult;
}

int main()
{
   freopen("input.txt","r",stdin);
   while(scanf("%d",&n) != EOF&& n != 0)
    {
      read_graph();
      int result = 0;
      result = prim();
      
      printf("The highest possible quality is1/%d.\n",result);
    }
    
    return0;
}


0 0