最小点覆盖数等于最大匹配数
此题为匈牙利算法模板题
///A机器有N种模式,B机器有M种模式
///K件产品,每件产品可以由a模式或者b模式完成
///问最少切换多少次模式可以完成K件产品
///解题思路: N种模式作为X集合,M种模式作为Y集合
///它们之间的连线表示两者取其中之一就可完成产品
///问题转化为最小点覆盖集,既求最大匹配数
///匈牙利算法求解二分图的最大匹配数
/*
#include <iostream>
#include<cstring>
using namespace std;
const int maxn = 105;
int g[maxn][maxn];
int x[maxn], y[maxn];
bool vis[maxn];
int n, m;
bool dfs(int u){
for(int v=1; v<=m; v++){
if(g[u][v] && !vis[v]){
vis[v] = true;
if(y[v] == -1 || dfs(y[v])){
y[v] = u;
x[u] = v;
return true;
}
}
}
return false;
}
int hungary(){
int ans = 0;
memset(x, -1, sizeof(x));
memset(y, -1, sizeof(y));
for(int u=1; u<=n; u++){
if(x[u] == -1){
memset(vis, false, sizeof(vis));
if(dfs(u)){
ans++;
}
}
}
return ans;
}
int main()
{
int k;
while(cin >> n){
memset(g, 0, sizeof(g));
if(n == 0)
break;
cin >> m >> k;
int j, u, v;
for(int i=0; i<k; i++){
cin >> j >> u >> v;
g[u][v] = 1;
}
int ans = hungary(u);
cout << ans << endl;
}
return 0;
}*/
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 200;
int linker[maxn];
int g[maxn][maxn];
bool vis[maxn];
int n, m;
int dfs(int u){
for(int v=1; v<=m; v++){
if(g[u][v] && !vis[v]){
vis[v] = true;
if(linker[v] == -1 || dfs(linker[v])){
linker[v] = u;
return 1;
}
}
}
return 0;
}
int hungary(int u){
int ans = 0;
memset(linker, -1, sizeof(linker));
for(int u=1; u<=n; u++){
memset(vis, false, sizeof(vis));
if(dfs(u))
ans++;
}
return ans;
}
int main(){
int k;
while(cin >> n && n){
cin >> m >> k;
memset(g, 0, sizeof(g));
int j, u, v;
for(int i=1; i<=k; i++){
cin >> j >> u >> v;
g[u][v] = 1;
}
int ans = hungary();
cout << ans << endl;
}
return 0;
}
///for job i, the constraint can be represent as a triple (i, x, y), which means it can be processed either
///in machine A at mode_x, or in machine B at mode_y.
/*
5 5 10
0 1 1
1 1 2
2 1 3
3 1 4
4 2 1
5 2 2
6 2 3
7 2 4
8 3 3
9 4 3
5 5 10
0 1 1
1 1 2
2 1 3
3 1 4
4 2 1
5 2 2
6 2 3
7 2 4
8 3 3
9 4 3
0
*/
/*
3
3
*/
linjiebiao ac code
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 200;
int linker[maxn];
bool vis[maxn];
struct node{
int to;
int cost;
node(int t, int c): to(t), cost(c){}
};
vector<node>g[maxn];
int n, m;
int dfs(int u){
for(int i=0; i<g[u].size(); i++){
node e = g[u][i];
if(e.cost && !vis[e.to]){
vis[e.to] = true;
if(linker[e.to] == -1 || dfs(linker[e.to])){
linker[e.to] = u;
return true;
}
}
}
return false;
}
int hungary(){
int ans = 0;
memset(linker, -1, sizeof(linker));
for(int u=1; u<=n; u++){
memset(vis, false, sizeof(vis));
if(dfs(u))
ans++;
}
return ans;
}
int main(){
int k;
while(cin >> n && n){
for(int i=0; i<maxn; i++){
g[i].clear();
}
cin >> m >> k;
int j, u, v;
for(int i=0; i<k; i++){
cin >> j >> u >> v;
g[u].push_back(node(v, 1));
}
int ans = hungary();
cout << ans << endl;
}
return 0;
}