HDU/HDOJ 3718 成都赛区2010 Similarity
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3718
But the LABEL of group doesn't make sense and the LABEL is just used to indicate different groups. So the representations {P,P,O,P,O,O,Q,Q,Q,Q} and {E,E,F,E,F,F,W,W,W,W} are equivalent to the original mapping sequence. However, the representations {A,A,A,A,B,B,C,C,C,C} and
{D,D,D,D,D,D,G,G,G,G} are not equivalent.
The pupils in class submit their mapping sequences and the teacher should read and grade the homework. The teacher grades the homework by calculating the maximum similarity between pupils' mapping sequences and the answer sequence. The definition of similarity is as follow.
Similarity(S, T) = sum(Si == Ti) / L
L = Length(S) = Length(T), i = 1, 2,... L,
where sum(Si == Ti) indicates the total number of equal labels in corresponding positions. The maximum similarity means the maximum similarities between S and all equivalent sequences of T, where S is the answer and fixed. Now given all sequences submitted by pupils and the answer sequence, you should calculate the sequences' maximum similarity.
题目很明显,就是利用KM算法。。这个很早就想到过了。
但是一直没有动手敲过这个题目。
今天鼓起勇气敲了下。。还是发现了不少问题。
首先是k和m输入反了,找了好久都不知道是哪儿WA了。。
然后就是建图,建图可以用26个顶点来建图。
其它的建图方式可能回超时。
我的代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<map>
#define inf 199999999
using namespace std;
int link[30],lx[30],ly[30];
bool x[30],y[30];
int net[30][30];
char s[10005];
bool dfs(int u)
{
int i;
x[u]=true;
for(i=0;i<26;i++)
{
if(lx[u]+ly[i]==net[u][i]&&!y[i])
{
y[i]=true;
if(link[i]==-1||dfs(link[i]))
{
link[i]=u;
return true;
}
}
}
return false;
}
int main()
{
int i,j,n,m,k,t,ii,jj;
char temp[2];
scanf("%d",&t);
while(t--)
{
memset(s,'\0',sizeof(s));
scanf("%d%d%d",&n,&k,&m);
for(i=1;i<=n;i++)
{
scanf("%s",temp);
s[i]=temp[0];
}
for(i=1;i<=m;i++)
{
memset(net,0,sizeof(net));
for(j=1;j<=n;j++)
{
scanf("%s",temp);
net[s[j]-'A'][temp[0]-'A']++;
}
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
memset(link,-1,sizeof(link));
memset(ly,0,sizeof(ly));
for(ii=0;ii<30;ii++)
lx[ii]=inf;
for(k=0;k<26;k++)
{
while(1)
{
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
if(dfs(k))
break;
int d=inf;
for(ii=0;ii<26;ii++)
{
if(x[ii])
{
for(jj=0;jj<26;jj++)
{
if(!y[jj]&&lx[ii]+ly[jj]-net[ii][jj]<d)
d=lx[ii]+ly[jj]-net[ii][jj];
}
}
}
for(ii=0;ii<26;ii++)
if(x[ii])
lx[ii]=lx[ii]-d;
for(ii=0;ii<26;ii++)
if(y[ii])
ly[ii]=ly[ii]+d;
}
}
int ans=0;
for(ii=0;ii<26;ii++)
ans=ans+net[link[ii]][ii];
printf("%.4lf\n",double(ans)/double(n));
}
}
return 0;
}