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 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
 题解:超时到爆,今天老是因为qsort出错,无语了额,最终还是会长帮忙检查了出来,哎~~~,qsort。。。。
kruskal代码:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <algorithm>
using namespace std;
const int MAXN=; int pre[MAXN],anser,num,N; struct Node{
int s,e,dis;
}; Node dt[MAXN*MAXN];
char str[MAXN][]; int gd(char *a,char *b){
int t=;
for(int i=;a[i];i++){
if(a[i]!=b[i])t++;
}
return t;
} int find(int x){
//return pre[x]= x==pre[x]?x:find(pre[x]);
int r=x;
while(r!=pre[r])r=pre[r];
int i=x,j;
while(i!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
/*void merge(Node a){
int f1,f2;
if(num==N)return;
if(pre[a.s]==-1)pre[a.s]=a.s;
if(pre[a.e]==-1)pre[a.e]=a.e;
f1=find(a.s);f2=find(a.e);
if(f1!=f2){num++;
pre[f1]=f2;
anser+=a.dis;
}
}*/
void initial(){
memset(pre,-,sizeof(pre));
//memset(dt,0,sizeof(dt));
//memset(str,0,sizeof(str));
anser=;
} //int cmp(const void *a,const void *b){
// if((*(Node *)a).dis<(*(Node *)b).dis)return -1;
// else return 1;
//} int cmp(Node a, Node b){
return a.dis < b.dis;
} int main(){
while(scanf("%d",&N),N){
num=;
initial();
for(int i=;i<N;i++){
scanf("%s",str[i]);
}
int k=;
for(int i=;i<N - ;i++){
for(int j=i+;j<N;j++){
dt[k].s=i;
dt[k].e=j;
dt[k].dis=gd(str[i],str[j]);
// printf("k==%d %d %d %d\n ",k,i,j,dt[k].dis);
k++;
}
}
sort(dt, dt + k, cmp);
//qsort(dt,k,sizeof(dt[0]),cmp);
int f1,f2;
for(int i=;i<k;i++){
if(pre[dt[i].s]==-)pre[dt[i].s]=dt[i].s;
if(pre[dt[i].e]==-)pre[dt[i].e]=dt[i].e;
f1=find(dt[i].s);
f2=find(dt[i].e);
if(f1!=f2){
num++;
pre[f1]=f2;
anser+=dt[i].dis;
}
if(num==N)break;
//merge(dt[i]);
}
printf("The highest possible quality is 1/%d.\n",anser);
}
return ;
}

prime代码:

 #include<stdio.h>
#include<string.h>
const int INF=0x3f3f3f3f;
const int MAXN=;
int map[MAXN][MAXN],low[MAXN];
int vis[MAXN];
int N,ans;
char str[MAXN][];
void prime(){
memset(vis,,sizeof(vis));
int temp,k,flot=;
vis[]=;
for(int i=;i<=N;i++)low[i]=map[][i];
for(int i=;i<=N;i++){
temp=INF;
for(int j=;j<=N;j++)
if(!vis[j]&&temp>low[j])
temp=low[k=j];
if(temp==INF){
printf("The highest possible quality is 1/%d.\n",ans);
break;
}
ans+=temp;
flot++;
vis[k]=;
for(int j=;j<=N;j++)
if(!vis[j]&&map[k][j]<low[j])
low[j]=map[k][j];
}
}
int fd(char *a,char *b){
int t=;
for(int i=;a[i];i++){
if(a[i]!=b[i])t++;
}
return t;
}
void initial(){
memset(map,INF,sizeof(map));
ans=;
}
int main(){int s;
while(~scanf("%d",&N),N){
initial();
for(int i=;i<=N;i++)scanf("%s",str[i]);
for(int i=;i<=N;i++){
for(int j=i+;j<=N;j++){
s=fd(str[i],str[j]);
if(s<map[i][j])map[i][j]=map[j][i]=s;
}
}
prime();
}
return ;
}

Truck History(kruskal+prime)的更多相关文章

  1. poj 1789 Truck History(kruskal算法)

    主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...

  2. Truck History(卡车历史)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26547   Accepted: 10300 Description Adv ...

  3. Truck History(poj 1789)

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  4. POJ 1789 Truck History(Prim+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  5. 最小生成树---普里姆算法(Prim算法)和克鲁斯卡尔算法(Kruskal算法)

    普里姆算法(Prim算法) #include<bits/stdc++.h> using namespace std; #define MAXVEX 100 #define INF 6553 ...

  6. Truck History(prime)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31871   Accepted: 12427 D ...

  7. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  8. POJ 1789:Truck History(prim&amp;&amp;最小生成树)

    id=1789">Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17610   ...

  9. Truck History(最小生成树)

    poj——Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27703   Accepted: 10 ...

随机推荐

  1. python连续爬取多个网页的图片分别保存到不同的文件夹

      python连续爬取多个网页的图片分别保存到不同的文件夹 作者:vpoet mail:vpoet_sir@163.com #coding:utf-8 import urllib import ur ...

  2. ubuntu下hadoop完全分布式部署

    三台机器分别命名为: hadoop-master ip:192.168.0.25 hadoop-slave1 ip:192.168.0.26 hadoop-slave2 ip:192.168.0.27 ...

  3. spring管理hibernate4 transaction getCurrentSession为什么报错?

    hibernate4不支持你用hibernate3的 getcurrentSession,建议你用openSession

  4. 【转】DynDNS使用随笔

    暂且小结一下: 1.下载编译客户端代码并交叉编译 首先,按照网上提示的步骤,在www.dyndns.com注册了帐号,并申请了域名,绑定了IP; 然后,在站点中找到客户端源码,其中ddclient是p ...

  5. Java专项面试训练(一)

    1.在Java中,( )类提供定位本地文件系统,对文件或目录及其属性进行基本操作( D ) A.FileInputStream B.FileReader C.FileWriter D.File解析:F ...

  6. android笔试题集2

    1.请谈一下Android系统的架构.答:Android系统采用了分层架构,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层. 2.谈谈android大众常用的五种布局.答 ...

  7. Ffmpeg和SDL如何同步视频(转)

    ong> PTS和DTS 幸运的是,音频和视频流都有一些关于以多快速度和什么时间来播放它们的信息在里面.音频流有采样,视频流有每秒的帧率.然而,如果我们只是简单的通过数帧和乘以帧率的方式来同步视 ...

  8. 放弃使用jQuery实现动画

    在Web开发的圈子里,开发人员经常觉得CSS动画是一种高性能web动画技术.假设想让网页载入的更快一些,就应该用纯CSS动画.事实上这样的观点是错误的,非常多开发人员早就放弃了javascript的动 ...

  9. web.xml中的主要元素说明(listener, filter, servlet)

    web.xml中加载的顺序为:context-param ---> listener ---> filter ---> servlet. listener:主要针对的是对象的操作,如 ...

  10. 代码初始化 故事板初始化 xib初始化总结

    对象的初始化有三种方式   // 代码创建 - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { ...