65,186
社区成员




#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
using namespace std;
const int maxn=1e6+5;
const int INF=0x3f3f3f3f;
typedef pair<int,int>pp;
int Map[2001][2001];
bool vis[maxn];
int dist[maxn];
int head[3*maxn];
int cnt ;
struct node
{
int data,to,next;
}e[maxn];
void add(int x,int y,int z)//前向星存图
{
e[cnt].to=y;
e[cnt].data=z;
e[cnt].next=head[x];
head[x]=cnt++;
}
int n,m;
char s[2005][8];
int get_distance(int a,int b)//求两点之间的距离
{
int ans =0;
for (int i=0;i<7;i++)
{
if(s[a][i]!=s[b][i])
ans++;
}
return ans;
}
void prim(int st)//优先队列优化,但是挂了
{
int i,j,k;
fill(vis,vis+maxn,0);
fill(dist,dist+maxn,INF);
priority_queue<pp,vector<pp>,greater<pp> >q;
dist[st]=0;
q.push(pp(0,st));
int ans =0;
while (!q.empty())
{
pp p=q.top();
int u=p.second;
if(vis[u])
continue;
ans+=dist[u];
vis[u]=1;
q.pop();
for (i=head[u];i!=-1;i=e[i].next)
{
node w=e[i];
if(dist[w.to]>w.data)
{
dist[w.to]=w.data;
q.push(pp(w.data,w.to));
}
}
}
cout<<"The highest possible quality is 1/"<<ans<<".\n";
}
void prim_less(int st)//普通算法
{
fill(dist,dist+maxn,INF);
fill(vis,vis+maxn,0);
int i,j,k;
dist[st]=0;
// vis[st]=1;
// for (i=0;i<n;i++)
// dist[i]=Map[st][i];
int ans =0;
while (1)
{
int minn=INF;
int u=-1;
for (i=0;i<n;i++)
{
if(minn>dist[i]&&!vis[i])
{
minn=dist[i];
u=i;
}
}
if(u==-1)
break;
vis[u]=1;
ans+=dist[u];
for (i=0;i<n;i++)
dist[i]=min(dist[i],Map[u][i]);
}
cout<<"The highest possible quality is 1/"<<ans<<".\n";
}
int main()
{
// std::ios::sync_with_stdio(0);
// cin.tie(0);
int x,y,z;
int i,j,k;
while (~scanf("%d",&n))
{
if(!n)
break;
cnt=0;
fill(head,head+maxn,-1);
for (i=0;i<n;i++)
scanf("%s",s[i]);
for (i=0;i<n;i++)
for (j=i+1;j<n;j++)
{
int temp=get_distance(i,j);
add(i,j,temp);
add(j,i,temp);
// Map[i][j]=Map[j][i]=temp;//prim_less
}
prim(0);
// prim_less(0);
}
return 0;
}