链接:http://poj.org/problem?id=1789
题意:懒得写。
思路:要让分式值最大,分母值就要最小。把卡车类型理解为无向网中的顶点,所求最佳方案即为求最小生成树,那个求和就是权值。每两个顶点间边的权值为对应两种卡车编码之间的距离,故先求出邻接矩阵。
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<string> #include<cmath> #include<map> using namespace std; const int INF=1000000; const int maxn=2000+5; const int codel=7+2; int n,sumw; int edge[maxn][maxn]; int lowcost[maxn]; char codes[maxn][codel]; void prim() { sumw=0; lowcost[0]=-1; for(int i=1;i<n;i++) lowcost[i]=edge[0][i]; for(int i=1;i<n;i++) { int min=INF,v; for(int j=0;j<n;j++)//j=1也可以,lowcost[0]为-1嘛 if(lowcost[j]!=-1 && lowcost[j]<min) { min=lowcost[j]; v=j; } sumw+=min; lowcost[v]=-1;// for(int j=0;j<n;j++) if(edge[v][j]<lowcost[j]) lowcost[j]=edge[v][j]; } } int main() { int d; while(scanf("%d",&n) && n) { for(int i=0;i<n;i++) scanf("%s",codes[i]); memset(edge,INF,sizeof(edge)); for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) { d=0; for(int k=0;k<7;k++) if(codes[i][k]!=codes[j][k]) d++;//if语句或写为 d+=codes[i][k]!=codes[j][k] edge[i][j]=edge[j][i]=d; } prim(); printf("The highest possible quality is 1/%d.\n",sumw); } return 0; }
写题的时候太不认真了,写到一半去聊qq。。。结果,各种小错误,都不好意思说了。。。以后闲聊者一律无视之。