zoj 2158 Truck History

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int inf=1<<24;
int dp[2000+5][2000+5];
char s[2000+5][20+5];
int main()
{
    int i,j,k,u,v,w,n,low[2000+5],q;
    while(~scanf("%d",&n))
    {
        //getchar();
        if(n==0) break;
        for(i=0;i<n;i++)
        {
           scanf("%s",s[i]);
        }

        for(i=0;i<n;i++)
            for(j=i;j<n;j++)
        {
            if(i==j) { dp[i][j]=inf;continue;}
            q=0;
            for(k=0;k<7;k++)
                if(s[i][k]!=s[j][k]) q++;
            dp[i][j]=dp[j][i]=q;
        }
        for(i=0;i<n;i++)
            low[i]=dp[0][i];
        low[0]=-1;
        q=0;
        for(i=1;i<n;i++)
        {
            int t=inf;
            for(j=0;j<n;j++)
            {
                if(low[j]!=-1&&low[j]<t)
                {
                    k=j;
                    t=low[j];
                }
            }
            q+=low[k];
            low[k]=-1;
            for(j=0;j<n;j++)
                low[j]=min(low[j],dp[k][j]);
        }
        printf("The highest possible quality is 1/%d.\n",q);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 04:38:39

zoj 2158 Truck History的相关文章

ZOJ 2158 &amp;&amp; POJ 1789 Truck History (经典MST)

链接:http://poj.org/problem?id=1789 或  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1158 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for br

poj 1789 Truck History 解题报告

题目链接:http://poj.org/problem?id=1789 题目意思:给出 N 行,每行7个字符你,统计所有的 行 与 行 之间的差值(就是相同位置下字母不相同),一个位置不相同就为1,依次累加.问最终的差值最少是多少. 额.....题意我是没看懂啦= =......看懂之后,就转化为最小生成树来做了.这是一个完全图,即每条边与除它之外的所有边都连通.边与边的权值是通过这个差值来算出来的. 1 #include <iostream> 2 #include <cstdio>

POJ1789 Truck History 【最小生成树Prim】

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18981   Accepted: 7321 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

Truck History(prim &amp; mst)

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19772   Accepted: 7633 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

Truck History(最小生成树)

poj——Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27703   Accepted: 10769 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for

Truck History

Truck History Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 59   Accepted Submission(s) : 21 Problem Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are u

POJ 1789 Truck History (Kruskal 最小生成树)

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19860   Accepted: 7673 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

POJ 1789 Truck History(最小生成树)

题意  有n辆卡车  每辆卡车用7个字符表示  输入n  再输入n行字符  第i行与第j行的两个字符串有多少个对应位置的字符不同  i与j之间的距离就是几  求连接所有卡车的最短长度  题目不是这个意思  这样理解就行了 prim啦啦啦啦 #include<cstdio> #include<cstring> using namespace std; const int N = 2005; int cost[N], dis[N][N], n, ans; void prim() { m

POJ 1798 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 of a truck. The code is simply a string of ex