一个二分图最大匹配的题;

匈牙利算法不熟;

建了个模,用最小费用最大流解决了

 #include <iostream>
#include <cstring>
#define INF 9999999
#include <cstdio>
#include <queue>
#include <vector>
#include<algorithm>
using namespace std;
#define maxn 6100 struct edge
{
int from,to,cap,flow,cost;
};
struct MCMF
{
int n,m,s,t;
vector<edge>edges;
vector<int>G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
void init(int n)
{
this->n=n;
for(int i=; i<n; i++)
G[i].clear();
edges.clear();
}
void addedge(int from,int to,int cap,int cost)
{
edges.push_back((edge){from,to,cap,,cost});
edges.push_back((edge){to,from,,,-cost});
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool bellman(int s,int t,int &flow,int &cost)
{
for(int i=; i<n; i++)d[i]=INF;
memset(inq,,sizeof(inq));
d[s]=;
inq[s]=;
p[s]=;
a[s]=INF; queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
inq[u] = ;
for(int i = ; i < G[u].size(); i++)
{
edge& e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to] = ;
}
}
}
}
if(d[t] == INF) return false;
flow += a[t];
cost += d[t]*a[t];
int u = t;
while(u != s)
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
int Mincost(int s, int t)
{
int flow = , cost = ;
while(bellman(s, t, flow, cost));
return cost;
}
};
int n;
int k,m;
char s2[],s1[];
int cur[][];
int tot[];
void first_solve()
{
memset(cur,,sizeof(cur));
memset(tot,,sizeof(tot));
for(int i=; i<=n; i++)
{
int k1=s1[i]-'A'+;
int k2=s2[i]-'A'+;
tot[k1]++;
cur[k1][k2]++;
}
}
MCMF solve;
int main()
{
int t;
int st=;
int final=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&k,&m);
for(int i=; i<=n; i++)
{
char s[];
scanf("%s",s);
s1[i]=s[];
}
for(int d=; d<=m; d++)
{
solve.init(final+);
for(int j=; j<=n; j++)
{
char s[];
scanf("%s",s);
s2[j]=s[];
}
first_solve();
for(int i=; i<=; i++)
solve.addedge(st,i,,);
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
int cnt=+j;
if (cur[i][j])
solve.addedge(i,cnt,,tot[i]-cur[i][j]);
else solve.addedge(i,cnt,,tot[i]);
}
}
for(int i=;i<=;i++)solve.addedge(i+,final,,);
int ans=n-solve.Mincost(st,final);
printf("%.4lf\n",(double)ans/(double)n);
}
}
return ;
}

hdu 3718的更多相关文章

  1. HDU 3718 Similarity(KM最大匹配)

    HDU 3718 Similarity 题目链接 题意:给定一个标准答案字符串,然后以下每一行给一个串.要求把字符一种相应一种,要求匹配尽量多 思路:显然的KM最大匹配问题,位置相应的字符连边权值+1 ...

  2. hdu 3718 Different Division

    Different Division Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. KM HDU 3718

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  4. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  5. HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...

  6. HDU 2639 01背包求第k大

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  8. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  9. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. Centos 6.5 安装 chrome , mplayer , alarm , clock

    http://my.oschina.net/jianweizhang/blog/306946 Chrome wget http://chrome.richardlloyd.org.uk/install ...

  2. 【转】物业管理与移动互联网科技|微信公众平台,物业app,物业O2O

    [导语]当下,物业管理行业正在接受新科技浪潮的冲击和洗礼,业界企业纷纷探索物业服务的新发展模式.云服务.微社区.微信公众平台.app等,这些本来陌生的词汇在物业管理行业变得耳熟能详.在借助科技手段拓展 ...

  3. JAXB - Annotations, The Annotation XmlElement

    The basic annotation for a field that's intended to be an element is XmlElement. It permits you to d ...

  4. Unity3D 之连按移动加速

    上代码: 效果是连续按W后,加速移动 为物体添加个拖拽效果,方便看运动轨迹. 将下面的脚本绑定到移动的物体上. 不过这里有一点很重要的需要去注意就是该方法不能放在 void FixedUpdate() ...

  5. postgresql 将同一个字段的值组合和将多个字段的值组合

    多字段值根据连接符拼接 concat_ws(':',aaa,bbb) 单字段值根据连接符拼接 string_agg(ccc,' \r\n ') 如果要将多个字段的值拼接成一个: string_agg( ...

  6. SignalR 跨域设置

    参考文章:http://www.cnblogs.com/nywd/p/3691813.html 上一节,已经实现了,当前域内的通信,这一节中,介绍一下跨域的即时通信,既然要做,我们肯定要把这个推送及聊 ...

  7. ### Theano

    Theano. #@author: gr #@date: 2014-07-02 #@email: forgerui@gmail.com 一.安装Theano ubuntu下安装相对简单. 安装依赖: ...

  8. 第十篇、自定义UIBarButtonItem和UIButton block回调

    // 自定义导航栏左边按钮 self.navigationItem.leftBarButtonItem = [JQBlockedBarButtonItem blockedBarButtonItemWi ...

  9. UITabBar - 深度解剖

    for (UIView *tabbarbutton in self.subviews) { //        NSLog(@"%@",tabbarbutton);         ...

  10. eNSP

    L2交换机 sysvlan 200q interface Vlanif 200ip address 192.168.0.1 24dis thisq interface Ethernet 0/0/1po ...