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. Grid++Report支持CS/BS模式的表报插件

    Grid++Report 可用于开发桌面C/S报表与WEB报表(B/S报表),C/S报表开发适用于VB.NET.C#.VB.VC.Delphi等.WEB报表开发适用于ASP.ASP.NET.JSP/J ...

  2. Elon Musk:同一时候颠覆几个行业的科技狂人

    在苹果著名的"不同凡想"广告中,赞扬了那些改变世界的疯狂家伙们."他们不喜欢墨守成规,也不愿安于现状","他们改变了事物","他们 ...

  3. Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 2

    Prepare 10g Database for OGG Create GGS and GGS_MON Database Users SQL> create tablespace ggs_tbs ...

  4. PHP: configure: error: mysql configure failed. Please check config.log for more information.

    为php增加mysql模块时报错 configure: error: mysql configure failed. Please check config.log for more informat ...

  5. 【欧拉函数】【HDU1286】 找新朋友

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. jquery优化引发的思考

    无意间看到jquery优化的一个细节让我觉得不可思议记录一下.仅仅只是换个地方代码就能提高数倍的效率,带给我的不是个仅是个小技巧,而是一总编程思想,技术大牛往往是在细节上体现. 通过缓存最小化选择操作 ...

  7. Html5 Css实现方形图片 圆形显示

    <!doctype html><html><head><meta charset="utf-8"><title>方形图片 ...

  8. net 2.0使用ajax

    asp.net ajax中用到了几个dll文件,这些可以从网上下载.http://ajax.asp.net站点下可以找到相关的下载.这其中包括:System.Web.Extensions.dll.Sy ...

  9. ASP.NET MVC中移除冗余Response Header

    本文主要介绍如何优化ASP.NET MVC使用IIS时Response Header中的不必要的信息 默认的,创建一个ASP.NET MVC项目,会在Response Header中包含一些敏感的信息 ...

  10. linux网络编程:select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET(转)

    从别人的博客中转载过来了这一篇文章,经过重新编辑排版之后展现于此,做一个知识点保存与学习. select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复 ...