POJ 1789 Truck History

本文介绍了一种使用最小生成树算法解决特定历史问题的方法。该问题涉及到一系列卡车类型的发展历程,通过计算不同卡车类型代码间的差异来构建最优的历史发展路径。
Truck History
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 26485 Accepted: 10279

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 exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types 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 distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of 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 plan such that to is 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 the codes of truck types, your program should find the highest possible quality of a derivation plan. 

Input

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

Output

For each test case, your program should output the text "The highest possible quality is 1/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个车牌号,刚开始只有一个车牌,其他车牌都是由一个车牌直接或间接产生 一个车牌到另一个车牌的产生权值是它们之间的数字不同的个数 问产生的最小的边权和
 1 #include<cstring>
 2 #include<iostream>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxn=2010;
 6 const int INF=0x3f;
 7 int map[maxn][maxn],dis[maxn],n;
 8 bool vis[maxn];
 9 char str[maxn][15];
10 int solve(){
11     for(int i=1;i<=n;i++) dis[i]=INF;
12     dis[1]=0;
13     memset(vis,0,sizeof(vis));
14     int MST=0;
15     while(1){
16         int Mix=INF,pos;
17         for(int i=1;i<=n;i++)
18           if(!vis[i]&&dis[i]<Mix)
19             Mix=dis[ pos=i ];
20         if(Mix == INF) break;
21         vis[pos]=true;
22         MST+=Mix;
23         for(int j=1;j<=n;j++)
24           dis[j]=min(dis[j],map[pos][j]);
25     }
26     return MST;
27 }
28 int main()
29 {
30     while(scanf("%d",&n)==1&& n!=0){
31         for(int i=1;i<=n;i++){
32             scanf("%s",str[i]+1);
33             for(int j=1;j<i;j++){
34                 int sum=0;
35                 for(int k=1;k<=7;k++)
36                   if(str[i][k] != str[j][k]) sum++;
37                 map[i][j]=map[j][i]=sum;
38             }
39         }
40         int ans=solve();
41         printf("The highest possible quality is 1/%d.\n" , ans);
42     }
43     return 0;
44 }

思路:见图,跑最小生成树,据说不加优化的Prim算法最优秀

Kursual版:

 

#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=2010;
const int INF=0x3f;
int n,tot,fa[maxn];
struct Edge{
    int from,to,value;
    bool operator < (const Edge &a) const{
        return value<a.value;
    }
}e[2000500];
char str[maxn][15];
int find(int x){
    if(x==fa[x]) return x;
    else return fa[x]=find(fa[x]);
}
int main()
{
    while(scanf("%d",&n)){
        if(n == 0) break;
        int Mst=0;tot=0;
        memset(e,0,sizeof(e));
        for(int i=1;i<=n;i++) scanf("%s",str[i]+1);
        for(int i=1;i<=n;i++) fa[i]=i;
        for(int i=1;i<n;i++){
            for(int j=i+1;j<=n;j++){
                int sum=0;
                for(int k=1;k<=7;k++)
                  if(str[i][k] != str[j][k]) sum++;
                e[++tot].from=i;e[tot].to=j;e[tot].value=sum;
            }
        }
        sort(e+1,e+tot+1);
        int cur=0;
        for(int i=1;i<=tot;i++){
            int u=e[i].from,v=e[i].to,w=e[i].value;
            int rx=find(u),ry=find(v);
            if(rx!=ry){
                fa[rx]=ry;
                Mst+=w;cur++;
            }
            if(cur == n-1) break;
        }
        printf("The highest possible quality is 1/%d.\n" ,Mst);
    }
    return 0;
}

 

注:上面标红色的那一句,我刚开始写在定义循环变量j那里,会导致WA掉。。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值