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 ...
随机推荐
- 本地环境phpStorm10+XDebug配置和断点调试
安装环境:XAMPP;phpStorm版本10; windows 7 64bit. XAMPP.phpStorm 都直接安装在了D盘根目录,9999m目录建在D:\xampp\htocts下,即目录工 ...
- Centos 6.5 安装 chrome , mplayer , alarm , clock
http://my.oschina.net/jianweizhang/blog/306946 Chrome wget http://chrome.richardlloyd.org.uk/install ...
- scala学习笔记:理解类继承
scala> import scala.reflect._ import scala.reflect._ scala> class Person(@BeanProperty var nam ...
- ASP.NET Web Froms开发模式中实现程序集的延迟加载
延迟加载是一个很大的诱惑,可以达到一些比较好的效果,比如: 1.在实体框架中,由于关联数据的数量和使用时机是不确定的,通过延迟加载,仅在使用的时候去执行关联数据的查询操作,减少无谓的数据查询操作,可以 ...
- 缓存(之一) 使用Apache Httpd实现http缓存
http://www.tuicool.com/articles/EFfeu2 HTTP性能的问题与方案 一个最终用户访问一个网页,从浏览器发出请求,到接受请求,时间大体上消耗在了以下几个部分: 建立t ...
- zxing.dll生成条码
引入zxing.dll using System; using System.Drawing; using ZXing.QrCode; using ZXing; using ZXing.Common; ...
- iOS 安全:UIWebView访问Https站点防止中间人攻击
尽管Https协议能够提供数据的加密.身份的认证等安全服务,但并不是没有漏洞.HTTPS协议安全隐患的存在可能使用户受到各种极具破坏力的网络攻击.其中中间人攻击(Man In The Middle, ...
- OpenJudge/Poj 1163 The Triangle
1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...
- Poj 1013 Counterfeit Dollar / OpenJudge 1013(2692) 假币问题
1.链接地址: http://poj.org/problem?id=1013 http://bailian.openjudge.cn/practice/2692 http://bailian.open ...
- Android中的EditText默认时不弹出软键盘的方法
方法一: 在 AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为 adjustUnspecified|stateHidden <a ...