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

匈牙利算法不熟;

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

 #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. ADS1.2 集成开发环境的使用

    连风大神都没用过这个ADS1.2,什么破玩意儿啊,好像板子里面的资料也没有找到有这个软件,但是网上有滴,ADS1.2 集成开发环境的使用还是要会的,,, ARM ADS 全称为ARM Develope ...

  2. 【Objective-C】0-第一个OC的类

    OC是一门面向对象的语言,因此它也有类.对象.静态\动态方法.成员变量的概念.这讲就来创建第一个OC的类. 一.语法简介 1.类 在Java中,我们用1个.java文件就可以描述清楚一个类:在OC中, ...

  3. python输出1到100之和的几种方法

    1. 使用内建函数range print sum(range(1,101)) 2. 使用函数reduce print reduce(lambda a,b:a+b,range(1,101)) 3. 使用 ...

  4. javascript 正则 验证 第25节

    <html> <head> <title>Form对象</title> <script type="text/javascript&qu ...

  5. cics下任务的停止

    cicsterm CEMT I TA TAB==找到要停止的进程 在进程后加入 f或者p 或者fp =========================== 或者找到进程ID号 用命令:kill -9 ...

  6. WIN7中oracle10g的安装注意事项

    1.本次安装数据库版本为10.2.0.1,操作系统版本为windows7 32位 2.注意在"setup.exe"中以右键属性后,设置以兼容模式及以管理员身份运行该程序:在%安装文 ...

  7. OpenCV2学习笔记03:Qt中配置OpenCV环境

    在Qt中开发基于OpenCV的应用时,需要配置对应函数库到环境变量,这时候我们需要使用到qmake能够识别的变量来指定环境变量. INCLUDEPATH: 用于指定搜索头文件到文件夹路径. LIBS: ...

  8. 【仿携程JQuery日期价格表】

    今天比较闲所以就花点时间又写了点东西. 相信这种价格表大家不会陌生 现在我就模仿它做一个简单版本的.效果如下 首先需要两个时间控件,我这里用的是HTML5里面的时间控件,这个没限制喜欢用什么就用什么 ...

  9. 目前IT行业的几个大方向

    我简单总结了一下目前it行业的8大方向:   1.嵌入式开发 传统的arm linux开发.新兴的智能硬件.物联网等技术的发展,都让整个方向成为热门领域.   2.游戏开发 cocos2d-x.uni ...

  10. R语言语法笔记

    ## 1. 数据输入 ## a$b # 数据框中的变量 a = 15 # 赋值 a <- 15 # 赋值 a = c(1,2,3,4,5) # 数组(向量) b = a[1] # 数组下标,从1 ...