二分图多重匹配问题
二分图多重匹配问题 #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 100005; const int maxm = 15; int cap[maxm]; int Link[maxm][maxn]; int vLink[maxm]; int mat[maxn][maxm]; int vis[maxm]; int n, m; bool Find(int u) { for(int i = 1; i <= m; i++) { if(!vis[i] && mat[u][i]) { int v = i; vis[v] = 1; if(vLink[v] < cap[v]) { Link[v][vLink[v]++] = u; return true; } for(int j = 0; j < vLink[v]; j++) { if(Find(Link[v][j])) { Link[v][j] = u; return true; } } } } return false; } bool solve() { memset(Link, 0, sizeof(Link)); memset(vLink, 0, sizeof(vLink)); for(int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); if(!Find(i)) return false; } return true; } int main() { while(EOF != scanf("%d %d",&n, &m)) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { scanf("%d",&mat[i][j]); } } for(int i = 1; i <= m; i++) { scanf("%d",&cap[i]); } if(solve()) puts("YES"); else puts("NO"); } return 0; }