Truck History

http://poj.org/problem?id=1789

#include<cstdio>

 #include<cstring>

 #include<algorithm>

 #define MAXN 2010

 

 const int INF=1<<28;

 using namespace std;

 int g[MAXN][MAXN];

 int dis[MAXN];

 bool vis[MAXN];

 int n,sum;

 char s[MAXN][8];

 int b[MAXN];

 bool sprim()

 {

     memset(vis,false,sizeof(vis));

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

         dis[i]=INF;

     dis[1]=0;sum=0;

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

     {

         int min=INF,k=0;

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

         {

             if(!vis[j]&&dis[j]<min)

             {

                 min=dis[j];

                 k=j;

             }

         }

         if(min==INF) return false;

         vis[k]=true;

         sum+=min;

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

         {

             if(!vis[j]&&dis[j]>g[k][j])

             {

                 dis[j]=g[k][j];

             }

         }

     }

     return true;

 }

 int main()

 {

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

 

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

        {

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

        }

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

        {

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

            {

                int ans=0;

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

                {

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

                      ans++;

                }

                g[i][j]=g[j][i]=ans;

            }

        }

        sprim();

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

     }

     return 0;

 

 }

 
View Code

 

你可能感兴趣的:(history)