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. Eclipse插件Mylyn管理上下文任务管理

    原文地址:http://www.ibm.com/developerworks/cn/java/j-mylyn1/ Mylyn 2.0,第 1 部分: 集成的任务管理 使用集成的 Eclipse 问题跟 ...

  2. UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>

    A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  3. 【转】DynDNS使用随笔

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

  4. No.26

    "信是未见之事的实底,是所望之事的确据".

  5. Python关于eval与json在字典转换方面的性能比较

    背景介绍 因为python中有eval()方法,可以很方便的将一些字符串类型与字典等数据结构之间进行转换, 所以公司的数据处理同事在保存一些特殊数据时就直接将字典的字符串保存在数据库中. 在程序中读取 ...

  6. DM6437 C64X+ EDMA 疑惑总结记录

    总结一下DM6437中的EDMA的使用出现的问题,方便以后再开发定位问题. 1.EDMA Link 和 Chain的区别 link实现了DMA的自动重加载(非静态模式),需要两个param chain ...

  7. 解决 Visual Studio 2012 有时不能调试的问题

    有时候发现 Visual Studio 2012 不能调试,有时候又能调试.感觉很烦,今天找到了一个解决办法,我也不知道为什么这样能解决. 问题: 解决:1. 找到 Properties ,双击 2. ...

  8. ORA-01034/ORA-27101解决

    sql> shutdown immediate 后就无法进行任何操作了,重新通过sqlplus不能登录,提示ORA-01034和ORA-27101错误 解决,以下全部在cmd中: 1. 启动or ...

  9. [转载]string转化大小写(C++)

    如何将一个字符串转换成大写或者小写?这是字符串匹配中经常需要做的事情,然而C++的Standard Library并没有提供将std::string转成大写和小写的功能,只有在提供将char转成大写( ...

  10. The port Command

    The port Command help: port help selfupdate selfupdate: sudo port selfupdate search: port search tft ...