poj 1789 Truck History

 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

1/Σ(to,td)d(to,td)


派生关系有向边,边权为两个串的不同字符的个数,要求的是找出一种用到所有串的派生关系,使得Q最小,(因为是1/Q),所以这题就是求最小生成树

最朴素的最小生成树AC

#include<stdio.h>
const int INF = 0x3f3f3f3f;
const int MAXN = 2000;
char word[MAXN][8];
int map[MAXN][MAXN];
int n, lowcost[MAXN], cloest[MAXN];
bool used[MAXN];
inline
int countDis(int i, int j){
int cnt = 0;
for(int k = 0; k < 7; ++k){
if(word[i][k] != word[j][k])
cnt
++;
}
return cnt;
}
int prim(){
int ans = 0, min, minidx;
for(int i = 0; i < n; ++i){
lowcost[i]
= map[0][i];
cloest[i]
= 0;
used[i]
= false;
}
used[
0] = true;
for(int i = 1; i < n; ++i){
min
= INF;
for(int j = 0; j < n; ++j){
if(!used[j] && lowcost[j] < min)
min
= lowcost[j], minidx = j;
}
used[minidx]
= true;
ans
+= min;
for(int j = 0; j < n; ++j){
if(!used[j] && lowcost[j] > map[minidx][j])
lowcost[j]
= map[minidx][j], cloest[j] = minidx;
}
}
return ans;
}
int main(){
while(scanf("%d",&n), n){
for(int i = 0; i < n; ++i){
scanf(
"%s",word[i]);
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
map[i][j]
= countDis(i, j);
}
}
printf(
"The highest possible quality is 1/%d.\n",prim());
}
return 0;
}

posted @ 2011-04-30 01:36  L..  阅读(131)  评论(0编辑  收藏  举报