匈牙利算法(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 ...
随机推荐
- DuiLib学习笔记(二) 扩展CScrollbar属性
DuiLib学习笔记(二) 扩展CScrollbar属性 Duilib的滚动条滑块默认最小值为滚动条的高度(HScrollbar)或者宽度(VScrollbar).并且这个值默认为16.当采用系统样式 ...
- linux网站服务Apache+php+mysql的安装
1.挂载光盘 自己习惯将光盘挂载在/media/cdrom目录,在做本地yum源的时候此目录为默认目录之一 [root@localhost /]# mount /dev/cdrom /media/cd ...
- Flash Download Failed-"Cortex-M3"
rror:Flash Download Failed-"Cortex-M3"出现一般有两种情况: 1.SWD模式下,Debug菜单中,Reset菜单选项(Autodetect/HW ...
- Linux 下从头再走 GTK+-3.0 (三)
之前我们为窗口添加了一个按钮,接下来让这个按钮丰富一点.并给窗口加上图标. 首先创建 example3,c 的源文件. #include <gtk/gtk.h> static void a ...
- IBM云的商业动作之我见(1):IBM 收购 OpenStack 托管私有云公司 Blue Box [IBM Acquired Blue Box]
2015-06-10 IBM 刚刚(2015/06/03)宣布收购 Blue Box 公司.本文就聊聊这点事. 1. Blue Box 是做什么的?它是一家中小型托管私有云提供商. 1.1 公司的简单 ...
- nodejs模块——http模块
http模块主要用于创建http server服务. 本文还用到url模块和path模块,还有fs模块.url模块用于解析url,path模块用于处理和转换文件路径. 一.简单应用 代码如下: // ...
- Caffe fine-tuning 微调网络
转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ 目前呢,caffe,theano,torch是当下比较流行的De ...
- DPM检测模型 VoC-release 5 linux 下编译运行
(转载请注明作者和出处 楼燚(yì)航的blog :http://www.cnblogs.com/louyihang-loves-baiyan/ 未经允许请勿用于商业用途) DPM目前使非神经网络方法 ...
- USACO八皇后
VIS 0 1 2分别竖线和两个对角线,参见对角线(x,y)的和 差关系表 #include<iostream> #include<cstdio> #include<al ...
- AC日记——紧急措施 openjudge 1.7 22
22:紧急措施 总时间限制: 1000ms 内存限制: 65536kB 描述 近日,一些热门网站遭受黑客入侵,这些网站的账号.密码及email的数据惨遭泄露.你在这些网站上注册若干账号(使用的用户 ...