二分匹配算法

来源:互联网 发布:ear cuff淘宝 编辑:程序博客网 时间:2024/06/07 15:27
匈牙利算法求二分匹配/***下#include<iostream>#include<vector>using namespace std;int V;                  //顶点数 vector<int> G[MAX_V];   //图的邻接表表示 int match[MAX_V];       //所匹配的顶点 bool used[MAX_V];       //DFS 中用到的访问标记//向图中增加一条连接 u 和 v 的边 void add_adge(int u, int v){G[u].push_back(v);G[v].push_back(u);}//通过 DFS 寻找增广路 bool dfs(int v){used[v] = true;for(int i = 0; i < G[v].size(); i++){int u = G[v][i], w = match[u];if(w < 0 || !used[w] && dfs(w)){match[v] = u;match[u] = v;return true;}}return false;}//求解二分图的最大匹配 int bipartite_mathcing(){int res = 0;memset(match, -1, sizeof(match));for(int v = 0; v < V; v++)if(match[v] < 0){memset(used, 0, sizeof(used));if(dfs(v))res++;}return res;}

0 0