poj1789Truck History

题目链接:http://poj.org/problem?id=1789

渣渣只会模板题。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=2010;
 6 int n;
 7 int f[maxn];
 8 char s[maxn][8];
 9 struct edge
10 {
11     int u,v,w;
12     bool operator<(const edge &a)
13     {
14         return w<a.w;
15     }
16 }e[maxn*maxn];
17 void init()
18 {
19     for(int i=0;i<=n;i++)
20         f[i]=i;
21 }
22 
23 int gf(int x)
24 {
25     return x==f[x]?x:f[x]=gf(f[x]);
26 }
27 void uni(int a,int b)
28 {
29     int pa=gf(a);
30     int pb=gf(b);
31     f[pb]=pa;
32 }
33 int main()
34 {
35     while(scanf("%d",&n)&&n)
36     {
37         init();
38         int cnt=0;
39         int ans=0;
40         for(int i=0;i<n;i++)
41             scanf("%s",s[i]);
42         for(int i=0;i<n;i++)
43             for(int j=i+1;j<n;j++)
44         {
45             int c=0;
46             for(int k=0;k<7;k++)
47             {
48                 if(s[i][k]!=s[j][k]) c++;
49             }
50             e[cnt].u=i;
51             e[cnt].v=j;
52             e[cnt].w=c;
53             cnt++;
54         }
55         sort(e,e+cnt);
56         for(int i=0;i<cnt;i++)
57         {
58             int x=e[i].u;
59             int y=e[i].v;
60             if(gf(x)!=gf(y)) {
61                 ans+=e[i].w;
62                 uni(x,y);
63             }
64         }
65         printf("The highest possible quality is 1/%d.
",ans);
66     }
67 }
原文地址:https://www.cnblogs.com/yijiull/p/6614119.html