您的位置:首页 > 其它

POJ_1789(Truck History )(最小生成树(普里姆prim算法))

2015-08-12 22:03 429 查看
POJ_1789(Truck History )
(最小生成树(普里姆prim算法)(从点入手)) 

Truck History

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 114   Accepted Submission(s) : 34
[align=left]Problem Description[/align]
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.

 
[align=left]Input[/align]
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.
 
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

 
[align=left]Sample Output[/align]

The highest possible quality is 1/3.

 

题意:
       每组测试数据有n个字符串,每个字符串有7个字符,两个字符串之间的distance代表这两个字符串之间不同字母的个数。一个字符串只能由另一个字符串“衍生”出来,代价是这两个字符串之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。
例如有如下4个编号:
aaaaaaa

baaaaaa

abaaaaa

aabaaaa

显然的,第二,第三和第四编号分别从第一编号衍生出来的代价最小,因为第二,第三和第四编号分别与第一编号只有一个字母是不同的,相应的distance都是1,加起来是3。也就是最小代价为3。问题可以转化为最小代价生成树的问题。
这道题关键就是转化为最小生成树问题。这道题和HDUOJ1875类似,HDU1875是把坐标转化为两岛之间的距离(可视为权值),而这道题可以把字符串的顺序号当作编号,把两个字符串之间不同的字符个数当作权值。
My   solution:
/*2015.8.12*/
#include<stdio.h>
#include<string.h>
#define INF  0x3F3F3F3F/*定义的无穷大*/
int dis[2200][2200],n;/*记录两个字符串之间不同字符的个数*/
int path[2200];/*集合外的所有点到集合(存放已选点)的最短距离*/
struct stu
{
int num;
char  c[10];
}node[2200];/*存放字符串并记录相应的序号(标号)*/
int zhuanhua(char a[],char b[])/*计算两个字符串之间的不同字符个数*/
{
int i,cnt=0;
for(i=0;i<7;i++)
if(a[i]!=b[i])
cnt++;
return cnt;
}

void prim()
{
int mark[2200],min,v,k=0,sum=0,i;
memset(mark,0,sizeof(mark));/*初始化标记数组*/
for(i=1;i<n;i++)
path[i]=dis[0][i];/*第一放入集合的点选择0号,因此path数组里存放的是集合外点到该集合的距离*/
mark[0]=1;                            /*也可以看作到0点的距离,因为此时集合只有一个点)*/
for(v=1;v<n;v++)
{
min=INF;/*这个绝对不能少,因为每次通过循环取最小值时,取的都是当前的数组中的最小值。若不加min=INF;,此时循环语句中的*/
for(i=1;i<n;i++)  /*min存放的是上一次循环中的最小值,在有些情况下最终循环结束也可能找不到最小值,导致结果错误*/
if(!mark[i]&&path[i]<min)/*取当前集合外(未被标记的点)到集合的距离最小的点*/
{
min=path[i];
k=i;/*记录这个最小点位置*/
}
mark[k]=1;/*标记这个最小点*/
for(i=1;i<n;i++)
{
if(!mark[i]&&path[i]>dis[k][i])
path[i]=dis[k][i];/*更新集合外的点到集合的最小距离(因为此时集合外的点添加到集合内了,所以需要更新)*/
}/*最终path数组中存放的是n-1个数字,即最短路径(n-1条边的和最小)*/
}
for(i=1;i<n;i++)
sum+=path[i];/*统计最小代价(最短路径总长度)*/
printf("The highest possible quality is 1/%d.\n",sum);
}

int main()
{
int i,j;
while(scanf("%d",&n)==1&&n)
{
getchar();
for(i=0;i<n;i++)
gets(node[i].c);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
dis[i][j]=dis[j][i]=zhuanhua(node[i].c,node[j].c);/*计算两个字符串之间的不同字符个数*/
prim();
}
return 0;
}

(这道题也可以用克鲁斯卡尔算法(从边入手)解)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: