Truck History
2015-08-11 16:37
375 查看
Truck History
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
Sample Output
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21508 | Accepted: 8361 |
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
Sample Output
The highest possible quality is 1/3.
//克鲁斯卡尔算法
/* 题意:: 给你n个车,每个车用7个字母表示,他们的差距可以用它们不同字母的个数表示,现在,要先找出它们 差距和的最小值。然后它们的最小和的倒数即为所求的最大值。 */ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int a[2100]; char b[2100][20];//用来存储字符串 int sum; struct zz { int f1,f2,d; }s[4000010]; int cmp(zz x,zz y)//按差距从小到大排序 { return x.d<y.d; } int find(int x) { while(x!=a[x]) x=a[x]; return x; } int marge(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(fx!=fy) { a[fx]=fy; return 1; } return 0; } int bj(char s1[],char s2[])//比较两个字符串不同字母的个数 { int cnt=0,i; for(i=0;i<7;i++) { if(s1[i]!=s2[i]) cnt++; } return cnt; } int main(){ int t,i,j,l,k,n; while(scanf("%d",&t),t) { for(i=1;i<=t;i++) a[i]=i; for(i=1;i<=t;i++) scanf("%s",b[i]); k=0; for(i=1;i<t;i++) //重点:记录两个串不同字母的个数 for(j=1+i;j<=t;j++) { s[k].f1=i;s[k].f2=j; s[k++].d=bj(b[i],b[j]); } sort(s,s+k,cmp); sum=0;n=0; for(i=0;i<k;i++) { if(marge(s[i].f1,s[i].f2)) { sum+=s[i].d; n++;//重点:用来判定是否已经成树, } if(n==t-1)//树的定义,n个点,n-1条边,如果已经完成,后面的即无需在加,节省时间,WA了两次 break; } if(n==t-1) printf("The highest possible quality is 1/%d.\n",sum); } return 0; }
<pre class="cpp" name="code">//prim算法 #include<stdio.h> #include<string.h> #define mx 0x3f3f3f3f char b[2100][20]; int n; int g[2010][2010],vis[2100],dis[2100]; int bj(char s1[],char s2[]) { int i,cnt=0; for(i=0;i<7;i++) { if(s1[i]!=s2[i]) { cnt++; } } return cnt; } void prim() { int i,j,k,min,sum=0; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { dis[i]=g[1][i]; } dis[1]=0; vis[1]=1; for(j=1;j<n;j++) { min=mx;k=1; for(i=1;i<=n;i++) { if(!vis[i]&&dis[i]<min) { min=dis[i]; k=i; } } sum+=min; vis[k]=1; for(i=1;i<=n;i++) { if(!vis[i]&&dis[i]>g[k][i]) dis[i]=g[k][i]; } } printf("The highest possible quality is 1/%d.\n",sum); } int main(){ while(scanf("%d",&n),n) { int i,j; memset(g,0,sizeof(g)); for(i=1;i<=n;i++) scanf("%s",b[i]); for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { g[i][j]=g[j][i]=bj(b[i],b[j]); } } prim(); } return 0; }
相关文章推荐
- opencv 读取摄像头并保存
- hdoj-1242-Rescue【广搜+优先队列】
- iOS入门(二十二)属性
- TreeSet 的两种比较性
- 更新日志 - 关于 iOS9 设备的安装及其他优化
- Redis主从复制
- select服务端代码实现
- UIImageVIew
- 通过均匀分布随机数产生器获得一个高斯分布随机数产生器
- UIButton&UILabel
- Nightmare
- POJ 3068 "Shortest" pair of paths(最小费用最大流-mcmf)
- xargs将stdin转换为命令行参数
- Android项目中如何用好构建神器Gradle?
- HTML语言学习网站
- 深入解析Oracle学习笔记(第五章)
- 反素数深度分析
- 24-IO流-64-IO流(操作数组的流)
- 24-IO流-65-IO流(编码表)
- fir.im Weekly - 我回来了