ZOJ 2158 POJ 1789 Truck History

最小生成树,主要是题目比较难懂。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int Maxn=+;
const int maxn=*+;
int Father[Maxn];
struct Edge
{
int from,to,w;
}edge[maxn];
int n,tot;
char s[maxn][]; int Find(int x)
{
if(x!=Father[x]) Father[x]=Find(Father[x]);
return Father[x];
} void init()
{
for(int i=;i<=n;i++) Father[i]=i;
tot=;
} bool cmp(const Edge&a,const Edge&b)
{
return a.w<b.w;
} int main()
{
freopen("in.txt","r",stdin);
while(~scanf("%d",&n)){
if(!n) break;
init();
for(int i=;i<n;i++)
scanf("%s",s[i]); for(int i=;i<n;i++)
for(int j=i+;j<n;j++)
{
edge[tot].from=i;
edge[tot].to=j;
edge[tot].w=;
for(int k=;k<;k++)
if(s[i][k]!=s[j][k])
edge[tot].w++;
tot++;
} sort(edge,edge+tot,cmp); int W=;
for(int i=;i<tot;i++)
{
int Fu=Find(edge[i].from);
int Fv=Find(edge[i].to);
if(Fu!=Fv)
{
Father[Fu]=Fv;
W=W+edge[i].w;
}
}
printf("The highest possible quality is 1/%d.\n",W);}
return ;
}
上一篇:JVM GC(整理)


下一篇:checkbox保存和赋值