2010成都站J题 ||hdu 3718 二分图的最佳匹配 =〉 最小费用最大流
来源:互联网 发布:五金小助手软件 编辑:程序博客网 时间:2024/06/05 09:14
http://acm.hdu.edu.cn/showproblem.php?pid=3718
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.
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
给定一个字符数列,后面m个同样长度的数列,对于每一个数列:每一个字母对应第一个数列中的一个字母,求怎样对应使得该数列有最多字母与第一个数列相应的匹配。例如:ABA CDC A对应C,B对应D,就完全对应匹配。再如:ABA FFE A对应E B对应F 就有2/3的对应。
解题思路:
二分图的最佳匹配。对母串中的任意一个与子串的不同字母一一对应用map[i][j]。我不会KM算法,两边加源点和汇点,转化为费用流来做的。
#include<cstdio>#include<iostream>#include<string.h>using namespace std;const int oo=1e9;const int mm=11111;const int mn=888;int node,src,dest,edge;int ver[mm],flow[mm],cost[mm],next[mm];int head[mn],dis[mn],p[mn],q[mn],vis[mn];void prepare(int _node,int _src,int _dest){ node=_node,src=_src,dest=_dest; for(int i=0; i<node; ++i)head[i]=-1,vis[i]=0; 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,ret=0,delta; //int ret1=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]; //ret1+=delta; } //printf("%d\n",ret1); return ret;}char a[10005][2],b[10005][2];int map[30][30];int main(){ int T,m,n,k; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&k,&m); for(int i=0; i<n; i++) scanf("%s",a[i]); while(m--) { prepare(54,0,53); memset(map,0,sizeof(map)); for(int i=0; i<n; i++) { scanf("%s",b[i]); map[a[i][0]-'A'+1][b[i][0]-'A'+1]++; } /* for(int i=1; i<=10; i++) { for(int j=1; j<=10; j++) printf("%d ",map[i][j]); printf("\n"); }*/ for(int i=1; i<=26; i++) { addedge(src,i,1,0); addedge(i+26,dest,1,0); for(int j=1; j<=26; j++) addedge(i,j+26,1,-map[i][j]); } //printf("%d\n",-SpfaFlow()); printf("%.4lf\n",-SpfaFlow()*1.0/n); } } return 0;}
0 0
- 2010成都站J题 ||hdu 3718 二分图的最佳匹配 =〉 最小费用最大流
- 线性规划与网络流24题之分配问题 最大费用最大流、最小费用最大流、二分图的最佳匹配
- [网络流24题] 18 分配问题(二分图最佳匹配,最小费用最大流)
- POJ2195 Going Home 【最小费用流】+【二分图最佳匹配】
- HDU 1853 Cyclic Tour【最小费用最大流 OR 带权二分图匹配】
- poj2195(二分图最大匹配,最小费用流)
- HDU 1533Going Home(KM算法求二分图最小权匹配或者最小费用最大流)
- HDU-1533 KM Going Home 最佳匹配 最小费用最大流
- HDU 1533 Going Home 最小费用最大流(入门)(模板)或者 二分匹配
- HDU 2426 Interesting Housing Problem 二分匹配(KM模板)或者最小费用最大流
- HDU 3488 Tour 最小费用最大流||最大匹配
- Regionals 2015 >> Asia - Tehran >> 7530 - Cafebazaar【二分图最佳匹配】【最小费用可行流】
- hdu 1150 二分图 最小点覆盖=最大匹配
- hdu 1853 Cyclic Tour(费用流OR二分图最佳匹配,5级)
- hdu 3395(费用流,二分图的最大权匹配)
- MZL's City (hdu 5352 最小费用流 ||二分图匹配)
- HDOJ 3488 - Tour 有向图分割成若干哈密顿回路(二分图的最小权匹配,最小费用最大流)...
- HDU 2063 过山车(匈牙利算法 二分图的最小顶点覆盖 二分图最大匹配)
- 求逆序数的三种数据结构比较
- STL算法归类
- 安装TortoiseGit 状态图标不能正常显示
- intellij idea常用设置
- 数据结构与算法
- 2010成都站J题 ||hdu 3718 二分图的最佳匹配 =〉 最小费用最大流
- sendBroadcastAsUser——Calling a method in the system process without a qualified user
- LeetCode——4Sum
- Lession 30 异常处理
- php xmlsimpleelement 解析xml
- 远程连接服务器For Windows 2003 & 2008
- ITEXT 用java写pdf文档
- java和c/c++的区别
- poj 1201(查分约束之spfa)