您的位置:首页 > 其它

poj 1789 最小生成树 prim kruskal

2015-08-22 20:05 302 查看
Truck History

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 21922Accepted: 8510
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


Sample Output
The highest possible quality is 1/3.


Source

CTU Open 200


#include<cstdio>
#include<cstring>
#include<iostream>
#define INF 0x3f3f3f3f

using namespace std;

char s[2010][10];
int edge[2010][2010];
int low[2010],next[2010];
int n;
int Prim()
{
int sum=0;
for(int i=0; i<n; i++)
{
next[i]=0;
low[i]=edge[0][i];
}
next[0]=-1;
for(int i=0; i<n-1; i++)
{
int Min=INF;
int v=-1;
for(int j=0; j<n; j++)
{
if(next[j]!=-1&&low[j]<Min)
{
Min=low[j];
v=j;
}
}
if(v!=-1)
{
//printf("%d %d %d\n",next[v],v,low[v]);
sum+=low[v];
next[v]=-1;
for(int j=0; j<n; j++)
{
if(next[j]!=-1&&low[j]>edge[v][j]) //next[j](不是v)
{
low[j]=edge[v][j];
next[j]=v;
}
}
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
while(~scanf("%d",&n))
{
if(!n)
break;
for(int i=0; i<n; i++)
{
scanf("%s",&s[i]);
}

for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
int num=0;
for(int k=0; k<7; k++)
{
if(s[i][k]!=s[j][k])
num++;
}
edge[i][j]=edge[j][i]=num;
}
Prim();
}
}

/*
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

struct node
{
int u,v,w;
} edge[3000000];    //数组开的太小了总是RE
char s[2010][10];
int bin[2100];
int ans,n;
int cmp(node p1,node p2)
{
return p1.w<p2.w;
}
int Find(int x)
{
int r=x;
while(r!=bin[r])
r=bin[r];
int j=x,i;
while(j!=r)
{
i=bin[j];
bin[j]=r;
j=i;
}
return r;
}
int Merge(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
bin[fx]=fy;
}
int Kruskal()
{
int sum=0,num=0;
for(int i=0; i<n; i++)
bin[i]=i;
// cout<<n;
for(int i=0; i<ans; i++)
{
int fx=edge[i].u;
// printf("%d\n",Find(fx));
int fy=edge[i].v;
// printf("%d",Find(fy));
if(Find(fx)!=Find(fy))
{

sum+=edge[i].w;
num++;
Merge(fx,fy);
}
if(num>=n-1)
break;
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
while(~scanf("%d",&n))
{
if(n==0)
break;
for(int i=0; i<n; i++)
scanf("%s",s[i]);
ans=0;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
int num=0;
for(int k=0; k<7; k++)
{
if(s[i][k]!=s[j][k])
num++;
}
edge[ans].u=i;
edge[ans].v=j;
edge[ans++].w=num;
}
sort(edge,edge+ans,cmp);
//cout<<n;
//            for(int i=0;i<ans;i++)
//				printf("%d %d %d\n",edge[i].u,edge[i].v,edge[i].w);
Kruskal();
}
}

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: