hdu 3718 Similarity(费用流应用)

来源:互联网 发布:还珠格格燕子复仇知画 编辑:程序博客网 时间:2024/06/03 11:04

Similarity

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 855    Accepted Submission(s): 319


Problem Description
When we were children, we were always asked to do the classification homework. For example, we were given words {Tiger, Panda, Potato, Dog, Tomato, Pea, Apple, Pear, Orange, Mango} and we were required to classify these words into three groups. As you know, the correct classification was {Tiger, Panda, Dog}, {Potato, Tomato, Pea} and {Apple, Pear, Orange, Mango}. We can represent this classification with a mapping sequence{A,A,B,A,B,B,C,C,C,C}, and it means Tiger, Panda, Dog belong to group A, Potato, Tomato, Pea are in the group B, and Apple, Pear, Orange, Mango are in the group C.
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.
 

Input
The input contains multiple test cases. The first line is the total number of cases T (T < 15). The following are T blocks. Each block indicates a case. A case begins with three numbers n (0 < n < 10000), k (0 < k < 27), m (0 < m < 30), which are the total number of objects, groups, and students in the class. The next line consists of n labels and each label is in the range [A...Z]. You can assume that the number of different labels in the sequence is exactly k. This sequence represents the answer. The following are m lines, each line contains n labels and each label also is in the range [A...Z]. These m lines represent the m pupils' answer sequences. You can assume that the number of different labels in each sequence doesn't exceed k.
 

Output
For each test case, output m lines, each line is a floating number (Round to 4 digits after the decimal point). You should output the m answers in the order of the sequences appearance.
 

Sample Input
210 3 3A A B A B B C C C CF F E F E E D D D DX X X Y Y Y Y Z Z ZS T R S T R S T R S3 2 2A B AC D CF F E
 

Sample Output
1.00000.70000.50001.00000.6667
 

Author
LIN, Yue
 

Source
2010 Asia Chengdu Regional Contest
 

Recommend
zhouzeyong
 
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3718
题意:给你一个答案串,还有m个串,求这m个串与答案串的最大相似度,字母可以等价替换
分析:我们可以把答案串A~Z当做点,目标串A~Z也当做点,那么可以先初始化s[i][j]表示把目标串i字符替换成j,和答案串的相同个数。。。那么每次只需要连一条目标串的i到答案串的j的边,容量为1,费用为相同个数。。。。然后虚拟源点和汇点到两端,容量为1,费用为0,做最大费用最大流即可= =
PS:这题有人说是费用流,如果我自己看的话,能不能想到呢= =
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int mm=66666;const int mn=5555;const int oo=1e9;int src,dest,node,edge;int ver[mm],cost[mm],flow[mm],next[mm];int head[mn],dis[mn],p[mn],q[mn];bool vis[mn]={0};void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0;i<node;++i)head[i]=-1;    edge=0;}void addedge(int u,int v,int f,int c){    ver[edge]=v,flow[edge]=f,cost[edge]=c,next[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,cost[edge]=-c,next[edge]=head[v],head[v]=edge++;}bool Spfa(){    int i,u,v,l,r=0,tmp;    for(i=0;i<node;++i)dis[i]=oo;    dis[q[r++]=src]=0;    p[src]=p[dest]=-1;    for(l=0;l!=r;(++l==mn)?l=0:l)        for(i=head[u=q[l]],vis[u]=0;i>=0;i=next[i])            if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i]))            {                dis[v]=tmp;                p[v]=i^1;                if(vis[v])continue;                vis[q[r++]=v]=1;                if(r==mn)r=0;            }    return p[dest]>-1;}int Spfaflow(){    int i,delta,ret=0;    while(Spfa())    {        for(i=p[dest],delta=oo;i>=0;i=p[ver[i]])            if(flow[i^1]<delta)delta=flow[i^1];        for(i=p[dest];i>=0;i=p[ver[i]])            flow[i]+=delta,flow[i^1]-=delta;        ret-=delta*dis[dest];    }    return ret;}char a[mm],b;int s[33][33];int main(){    int i,j,k,n,m,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&k,&m);        for(i=0;i<n;++i)            scanf(" %c",&a[i]);        while(m--)        {            memset(s,0,sizeof(s));            for(i=0;i<n;++i)            {                scanf(" %c",&b);                ++s[b-'A'][a[i]-'A'];            }            prepare(54,0,53);            for(i=0;i<26;++i)            {                addedge(src,i+1,1,0);                addedge(i+27,dest,1,0);            }            for(i=0;i<26;++i)                for(j=0;j<26;++j)                    if(s[i][j])addedge(i+1,j+27,1,-s[i][j]);            printf("%.4lf\n",Spfaflow()*1.0/(1.0*n));        }    }    return 0;}


原创粉丝点击