Truck History(kruskal+prime)
Truck History
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 59 Accepted Submission(s) : 21
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.
#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)的更多相关文章
- poj 1789 Truck History(kruskal算法)
主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...
- Truck History(卡车历史)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26547 Accepted: 10300 Description Adv ...
- Truck History(poj 1789)
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- POJ 1789 Truck History(Prim+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- 最小生成树---普里姆算法(Prim算法)和克鲁斯卡尔算法(Kruskal算法)
普里姆算法(Prim算法) #include<bits/stdc++.h> using namespace std; #define MAXVEX 100 #define INF 6553 ...
- Truck History(prime)
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31871 Accepted: 12427 D ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- POJ 1789:Truck History(prim&&最小生成树)
id=1789">Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17610 ...
- Truck History(最小生成树)
poj——Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27703 Accepted: 10 ...
随机推荐
- python连续爬取多个网页的图片分别保存到不同的文件夹
python连续爬取多个网页的图片分别保存到不同的文件夹 作者:vpoet mail:vpoet_sir@163.com #coding:utf-8 import urllib import ur ...
- ubuntu下hadoop完全分布式部署
三台机器分别命名为: hadoop-master ip:192.168.0.25 hadoop-slave1 ip:192.168.0.26 hadoop-slave2 ip:192.168.0.27 ...
- spring管理hibernate4 transaction getCurrentSession为什么报错?
hibernate4不支持你用hibernate3的 getcurrentSession,建议你用openSession
- 【转】DynDNS使用随笔
暂且小结一下: 1.下载编译客户端代码并交叉编译 首先,按照网上提示的步骤,在www.dyndns.com注册了帐号,并申请了域名,绑定了IP; 然后,在站点中找到客户端源码,其中ddclient是p ...
- Java专项面试训练(一)
1.在Java中,( )类提供定位本地文件系统,对文件或目录及其属性进行基本操作( D ) A.FileInputStream B.FileReader C.FileWriter D.File解析:F ...
- android笔试题集2
1.请谈一下Android系统的架构.答:Android系统采用了分层架构,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层. 2.谈谈android大众常用的五种布局.答 ...
- Ffmpeg和SDL如何同步视频(转)
ong> PTS和DTS 幸运的是,音频和视频流都有一些关于以多快速度和什么时间来播放它们的信息在里面.音频流有采样,视频流有每秒的帧率.然而,如果我们只是简单的通过数帧和乘以帧率的方式来同步视 ...
- 放弃使用jQuery实现动画
在Web开发的圈子里,开发人员经常觉得CSS动画是一种高性能web动画技术.假设想让网页载入的更快一些,就应该用纯CSS动画.事实上这样的观点是错误的,非常多开发人员早就放弃了javascript的动 ...
- web.xml中的主要元素说明(listener, filter, servlet)
web.xml中加载的顺序为:context-param ---> listener ---> filter ---> servlet. listener:主要针对的是对象的操作,如 ...
- 代码初始化 故事板初始化 xib初始化总结
对象的初始化有三种方式 // 代码创建 - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { ...