Truck History---poj1789

题目链接:http://poj.org/problem?id=1789

 

题意: They defined the distance of truck types as the number of positions with different letters in truck type codes

 

这句话说明距离被定义成编号之间字母的不同个数;编号是由7个字符构成的字符串;

 

然后求最小生成树;

 

#include<stdio.h>

#include<string.h>

#include<map>

#include<iostream>

#include<algorithm>

#include<math.h>

#define N 2010

#define INF 0xfffffff



using namespace std;



int maps[N][N],vis[N],dist[N], n;



int Prim(int start)

{

    int ans=0;

    vis[start] = 1;

    for(int i=1; i<=n; i++)

    {

        dist[i] = maps[start][i];

    }

    for(int i=1; i<=n; i++)

    {

        int Min = INF, index = -1;

        for(int j = 1; j <= n; j++)

        {

            if(vis[j]==0 && Min>dist[j])

            {

                Min = dist[j];

                index = j;

            }

        }

        if(index == -1) break;

        ans += Min;

        vis[index] = 1;

        for(int j=1; j<=n; j++)

        {

            if(vis[j]==0 && dist[j] > maps[index][j])

                dist[j] = maps[index][j];

        }

    }

    return ans;

}



int main()

{

    int i, j, k;

    char s[N][20];

    while(scanf("%d", &n), n)

    {

        for(i=1; i<=n; i++)

        {

            scanf("%s", s[i]);

            vis[i] = 0;

            dist[i] = INF;

            for(j=1; j<=n; j++)

                maps[i][j] = INF;

        }



        for(i=1; i<=n; i++)

        {

            for(j=1; j<i; j++)

            {

                int cnt = 0;

                for(k=0; k<7; k++)

                    if(s[i][k] != s[j][k])

                        cnt++;

                maps[i][j] = maps[j][i] = cnt;

            }

        }

        int ans;

        ans = Prim(1);

        printf("The highest possible quality is 1/%d.\n",  ans);

    }

    return 0;

}

 

你可能感兴趣的:(history)