hdu 3718
一个二分图最大匹配的题;
匈牙利算法不熟;
建了个模,用最小费用最大流解决了
#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的更多相关文章
- HDU 3718 Similarity(KM最大匹配)
HDU 3718 Similarity 题目链接 题意:给定一个标准答案字符串,然后以下每一行给一个串.要求把字符一种相应一种,要求匹配尽量多 思路:显然的KM最大匹配问题,位置相应的字符连边权值+1 ...
- hdu 3718 Different Division
Different Division Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- KM HDU 3718
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 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 ...
- HDU 2639 01背包求第k大
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- MongoDB基本操作
转:http://zhidao.baidu.com/link?url=D5s4tNnP6hH0XPZkFooV-o4MQH3pNZh7C3rtLX_HtVWaIyBRhLIUyoZYfVv15l2eS ...
- xml约束DTD演示
此演示xml和DTD在一个文件中 book.xml <?xml version="1.0" encoding="utf-8"?> <!DOCT ...
- scala学习笔记:match与unapply()
编写如下代码: object MatchTest { def foo(a : Any) : String = { a match { case 1 => "int:1" ca ...
- asp.net 配置二级域名的共享session,并实现sso单点登录
公司最近做了一个新网站.原先网站的网址是www.xxxx.com.新做的网站要部署到info.xxxx.com.这两个网站要实现单点登录.而新老网站本身机构的原因,对于登录状态的判断,说白了就是对于s ...
- notepad++中的python缩进问题
现在并没有遇到什么卵问题,但查到资料说会有问题,先记一笔,tab自动换4个空格 cmd /k "$(FULL_CURRENT_PATH)" & PAUSE & EX ...
- try catch异常捕获
格式为: try { int i = int.Parse(Console.ReadLine()); //容易发生错误的语句 ...
- NSDate与 NSString 、long long类型的相互转化
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3560280.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- python isinstance 判断各种类型的小细节
1. 基本语法 isinstance(object, classinfo) Return true if the object argument is an instance of the class ...
- 转载:mysql 对于百万 千万级数据的分表实现方法
一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法.目前我所知道的方法都是MYISAM的,INNODB如何做分表并且保留事务和外键,我还不是 ...
- Android Studio第一次提交git使用
安装: Git客户端网址:http://git-scm.com/download/ 根据自己的使用平台下载对应的客户端.这里以Mac系统为例,当客户端软件安装配置完毕后,打开AS的配置面板,找到Git ...