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中用SWT设计界面

    摘自http://www.tulaoshi.com/n/20160129/1488574.html 在Eclipse中用SWT设计界面 1. 为什么要使用SWT?  SWT是一个IBM开发的跨平台GU ...

  2. 六度分离(floyd算法+dijskra+SPFA)

    六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. Windows Server 2008 R2 开启Win7主题效果Aero

    1.打开 开始---管理工具----服务器管理器--功能 2.点击 “添加功能”,选择“桌面体验”,这样就会安装上win7 主题和Windows media player 3.重启电脑后,在“服务”里 ...

  4. MyEclipse 注册码

    MyEclipse 注册码和大家共享一下! 一:MyEclipse_6.0.1GA_E3.3.1_FullStackInstaller注册码 Subscriber:javp Subscription ...

  5. Bestcoder HDU5059 Help him 字符串处理

    Help him Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. JavaScript面向对象精要

    来自:淡忘~浅思. 链接:http://www.ido321.com/1585.html 和 http://www.ido321.com/1586.html 数据类型   在JavaScript中,数 ...

  7. win8下 web测试 之 hosts绑定

    从这个开始,开启web测试之旅 绑定hosts: 1.在C:\Windows\System32\drivers\etc下找到 hosts 文件 2.将hosts文件复制到一个地方: 3.修改hosts ...

  8. mvc ajax请求

    @{ ViewBag.Title = "ajax"; } <script src="../../Scripts/jquery-1.4.4.js" type ...

  9. Winform获取应用程序的当前路径的方法集合(转)

    Winform获取应用程序的当前路径的方法集合,具体如下,值得收藏 //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembly.Loc ...

  10. HDU 5730 - Shell Necklace

    题意: 给出连续的1-n个珠子的涂色方法 a[i](1<=i<=n), 问长度为n的珠链共有多少种涂色方案 分析: 可以得到DP方程: DP[n] = ∑(i=1,n) (DP[n-i]* ...