Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15326 | Accepted: 5886 |
Description
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
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
Output
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
這道題不用看,最小生成樹,prim算法,不同卡車是節點,然後密碼相錯多少是路徑,直接先寫一個函數,判定路徑長度,任意兩點距離,即任意兩種卡車密碼錯位數多少,圖建立起來就是將算法帶入,在最小生成樹prim函數裏面加一步,求出任意路徑算出路徑的長度並且全部加起來,返回這個值,直接輸出就可以AC了,還是比較簡單,難度就是修改prim函數。再基本木有難度了。。。
下面是AC代碼:
#include<cstdio>
#include<iostream>
#include<cstring>
#define infinity 1000000
#define max_vertexes 2012
using namespace std;
int G[max_vertexes][max_vertexes];
char ch[2012][10];
int prim(int vcount)
{
int i,j,k,sum=0;
int lowcost[max_vertexes],closeset[max_vertexes],used[max_vertexes];
for (i=0;i<vcount;i++)
{
lowcost[i]=G[0][i];
closeset[i]=0;
used[i]=0;
}
used[0]=1;
for (i=1;i<vcount;i++)
{
j=0;
while (used[j]) j++;
for (k=0;k<vcount;k++)
if ((!used[k])&&(lowcost[k]<lowcost[j])) j=k;
sum+=G[j][closeset[j]];
used[j]=1;
for (k=0;k<vcount;k++)
if (!used[k]&&(G[j][k]<lowcost[k]))
{ lowcost[k]=G[j][k];
closeset[k]=j; }
}
return sum;
}
void init(int n)
{
int i,j,k;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
for(k=0;k<7;k++)
if(ch[i][k]!=ch[j][k])
{
G[i][j]++;
G[j][i]++;
}
}
int main()
{
int n,i;
while(1)
{
scanf("%d",&n);
if(n==0)
break;
memset(G,0,sizeof(G));
for(i=0;i<n;i++)
scanf("%s",ch[i]);
init(n);
printf("The highest possible quality is 1/%d.\n",prim(n));
}
return 0;
}
今天主頁君很高興,在POJ上面AC了三道題,並且都是1A的,很高興啊!!!