匈牙利算法(codevs2776)
type node=^link;
link=record
des:longint;
next:node;
end; var
n,m,i,t,num:longint;
p:node;
nd:array[..] of node;
mat:array[..] of longint;
vis:array[..] of boolean;
sel:array[..] of longint; function find(po:longint):boolean;
var
p:node;
begin
p:=nd[po];
while p<>nil do
begin
if vis[p^.des]=false then
begin
vis[p^.des]:=true;
if sel[p^.des]= then
begin
sel[p^.des]:=po;
mat[po]:=p^.des;
exit(true);
end else
if find(sel[p^.des])=true then
begin
sel[p^.des]:=po;
mat[po]:=p^.des;
exit(true);
end;
end;
p:=p^.next;
end;
exit(false);
end; begin read(n,m); for i:= to n do
begin
read(t);
while t<> do
begin
new(p);p^.next:=nd[t];p^.des:=i;nd[t]:=p;
read(t);
end;
end; for i:= to m do
if mat[i]= then
begin
fillchar(vis,sizeof(vis),);
if find(i) then inc(num);
end; writeln(num); end.
c++
-------------------------------------------------------------------------------------------
BZOJ1059
#include <cstdio>
int nd[],next[],bt[],des[],sel[],mat[],n,cnt;
int dfs(int po){
for (int p=nd[po];p!=-;p=next[p])
if (bt[des[p]]==){
bt[des[p]]=;
if (sel[des[p]]==-||dfs(sel[des[p]])){
sel[des[p]]=po;
mat[po]=des[p];
return();
}
}
return();
}
int Hung(){
for (int i=;i<=n;i++) sel[i]=-;
for (int i=;i<=n;i++){
for (int j=;j<=n;j++) bt[j]=;
if (!dfs(i)) return();
}
return();
}
void addedge(int x,int y){
next[++cnt]=nd[x];des[cnt]=y;nd[x]=cnt;
}
int main(){
int T;
scanf("%d",&T);
while (T--){
cnt=;
scanf("%d",&n);
for (int i=;i<=n;i++) nd[i]=-;
for (int i=;i<=n;i++)
for (int j=;j<=n;j++){
int t;
scanf("%d",&t);
if (t) addedge(i,j);
}
if (Hung()) printf("Yes\n");else printf("No\n");
}
}
另外,
最小点覆盖数=最大匹配数
最大独立集=顶点数-最大匹配数
最小路径覆盖数 = 顶点数 - 最大匹配数
_____________________________________________
BZOJ1443
确定一个点是否为匹配的必选点,后手走匹配边
(确定必选边BZOJ2140)
#include <cstdio>
#include <map>
using namespace std; map <int,int> mp; const int dir[][]={{-,},{,-},{,},{,}}; int n,m,b[][],ndx[],ndy[],next[],des[];
int vis[],sel[],mat[],dfstim,xcnt,ycnt,cnt,ans[][];
char st[]; void addedgex(int x,int y){
next[++cnt]=ndx[x];des[cnt]=y;ndx[x]=cnt;
} void addedgey(int x,int y){
next[++cnt]=ndy[x];des[cnt]=y;ndy[x]=cnt;
} int ok(int x,int y){
return(x&&y&&x<=n&&y<=m&&b[x][y]);
} int dfs(int po){
for (int p=ndx[po];p!=-;p=next[p])
if (!vis[des[p]]){
vis[des[p]]=;
if (sel[des[p]]==||dfs(sel[des[p]])){
mat[po]=des[p];sel[des[p]]=po;
return();
}
}
return();
} int checkx(int po){
vis[po]=dfstim;
if (!mat[po]) return();
for (int p=ndy[mat[po]];p!=-;p=next[p])
if (vis[des[p]]<dfstim)
if (checkx(des[p])) return();
return();
} int checky(int po){
vis[po]=dfstim;
if (!sel[po]) return();
for (int p=ndx[sel[po]];p!=-;p=next[p])
if (vis[des[p]]<dfstim)
if (checky(des[p])) return();
return();
} int main(){
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++){
scanf("%s",&st);
for (int j=;j<=m;j++)
if (st[j-]=='.'){
b[i][j]=;
if (((i+j)&)==){
mp[i*+j]=++xcnt;
}else{
mp[i*+j]=++ycnt;
}
}
} for (int i=;i<=xcnt;i++) ndx[i]=-;
for (int i=;i<=ycnt;i++) ndy[i]=-;
for (int i=;i<=n;i++) for (int j=;j<=m;j++)
if (b[i][j]&&(((i+j)&)==))
for (int k=;k<;k++)
if (ok(i+dir[k][],j+dir[k][]))
addedgex(mp[i*+j],mp[(i+dir[k][])*+j+dir[k][]]),
addedgey(mp[(i+dir[k][])*+j+dir[k][]],mp[i*+j]); for (int i=;i<=xcnt;i++){
for (int j=;j<=ycnt;j++) vis[j]=;
dfs(i);
} cnt=;dfstim=;
for (int i=;i<=n;i++) for (int j=;j<=m;j++)
if (b[i][j]){
dfstim++;
int po=mp[i*+j];
if (((i+j)&)==){
if (checkx(po)){
cnt++;
ans[cnt][]=i;ans[cnt][]=j;
}
}else
if (checky(po)){
cnt++;
ans[cnt][]=i;ans[cnt][]=j;
}
} if (cnt) printf("WIN\n");else printf("LOSE\n");
for (int i=;i<=cnt;i++)
printf("%d %d\n",ans[i][],ans[i][]);
}
——————————————————————————————
霍尔定理
二部图G中的两部分顶点组成的集合分别为X, Y, X={X1, X2, X3,X4, .........,Xm} , Y={y1, y2, y3, y4 , .........,yn},G中有一组无公共点的边,一端恰好为组成X的点的充分必要条件是: X中的任意k个点至少与Y中的k个点相邻。
TCO15 Round 1A Revmatching
You have a weighted bipartite graph. Each partition contains n vertices numbered 0 through n-1. You are given the weights of all edges encoded into a vector <string> A with n elements, each containing n characters. For each i and j, A[i][j] is '0' if there is no edge between vertex i in the first partition and vertex j in the second partition. Otherwise, A[i][j] is between '1' and '9', inclusive, and the digit represents the weight of the corresponding edge.
A perfect matching is a permutation p of 0 through n-1 such that for each i there is an edge (of any positive weight) between vertex i in the first partition and vertex p[i] in the second partition.
Your goal is to have a graph that does not contain any perfect matching. You are allowed to delete edges from your current graph. You do not care about the number of edges you delete, only about their weights. More precisely, you want to reach your goal by deleting a subset of edges with the smallest possible total weight. Compute and return the total weight of deleted edges in an optimal solution.
枚举每个X的子集,设其包含k个元素。断开若干点后使得Y中仅有k-1个元素。将断开点的价值求出后排序即可
int smallest(vector <string> A){
int n=A.size();
int ans=1e9;
for (int i=;i<<<n;i++){
for (int j=;j<=n;j++) sum[j]=;
for (int j=;j<=n;j++)
if (i&(<<(j-)))
for (int k=;k<=n;k++)
sum[k]+=A[j-][k-]-'';
sort(sum+,sum+n+);
int t=bitcount(i),tsum=;
for (int j=;j<=n-t+;j++) tsum+=sum[j];
ans=min(ans,tsum);
}
return(ans);
}
匈牙利算法(codevs2776)的更多相关文章
- ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)
//匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring&g ...
- 匈牙利算法——S.B.S.
匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...
- 匈牙利算法与KM算法
匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...
- HDU1054 Strategic Game——匈牙利算法
Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...
- poj1274(匈牙利算法)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22809 Accepted: 101 ...
- 匈牙利 算法&模板
匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...
- 【入门】匈牙利算法+HNOI2006 hero超级英雄
一.关于匈牙利算法 匈牙利算法是由匈牙利数学家Edmonds提出的,用增广路径求二分图最大匹配的算法. 听起来高端,其实说白了就是: 假设不存在单相思(单身狗偷偷抹眼泪),在一个同性恋不合法的国家里( ...
- [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)
描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
- UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法
二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...
- Poj(1466),最大独立集,匈牙利算法
题目链接:http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total S ...
随机推荐
- Python 标准异常
异常名称 描述 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常规错误的 ...
- raspberry pi2 智能小车源码及测试视频
作者:XIAOBO QQ:463431476 转载请注明作者Python 源代码 import RPi.GPIO as GPIO #human-computer-interaction import ...
- Arch Linux sudo: PAM authentication error: Module is unknown [Solved!]
问题描述: 我的 Arch Linux 已经用了快半年多,由于 Arch Linux 的滚挂问题,我从没有直接升级过系统.软件版本以及库自然落后了一些. 就在我准备需要用到 NFS 时,挂载网络文件系 ...
- 网页中显示pdf
1.<embed width="800" height="600" src="test_pdf.pdf"> </embed ...
- 谈谈Lucene和Solr索引存目录
在Lucene中,有几种索引存放模式呢?用过的人可能记得SimpleFSDirectory.MMapDirectory.NIOFSDirectory.RAMDirectory这四种.新版本的通过FSD ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- NOIP2000单词接龙[DFS]
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...
- 一句话爆破速度提升一千倍python脚本
这个脚本是接地提供的思路,一句话爆破速度提升一千倍,看了他的帖子然而没有看到工具,思路很牛逼,我提供一个Python脚本本地测试了下,十万密码只需要3秒,速度还是可以的 # coding:utf-8 ...
- git删除分支|查看分支动态
git不能在当前分支下删除你当前所在的分支,比如你要删除new分支,而现在正在处于new分支下,则执行git branch -d new的时候会报错 error: Cannot delete bran ...
- grunt-contrib-uglify压缩插件的常用配置属性
mangle:false(不混淆变量名和方法名,保留原有的名字),如果不配置,默认混淆,就是将所有方法名和变量都用a,b,c等字母替换 preserveComments : 'all'(不删除注释,还 ...