1. 程式人生 > >hdu 1150(最小點覆蓋)

hdu 1150(最小點覆蓋)

傳送門
二分圖最大匹配=最小點覆蓋
題解:二分圖左邊點集為機器A的模式,右邊為機器B的模式。對於每個任務,左右連邊,最後跑一遍二分圖最大匹配(最小點覆蓋)即可。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=104;
int n,m,k;
bool vis[MAXN<<1];
int mat[MAXN<<1];
int head[MAXN<<1
],etot; struct EDGE { int v,nxt; }e[MAXN*20]; bool dfs(int p) { for (int i=head[p];~i;i=e[i].nxt) { int v=e[i].v; if (!vis[v]) { vis[v]=true; if (mat[v]==-1||dfs(mat[v])) { mat[v]=p; return true; } } } return
false; } inline void adde(int u,int v) { e[etot].nxt=head[u],e[etot].v=v,head[u]=etot++; e[etot].nxt=head[v],e[etot].v=u,head[v]=etot++; } int main() { while (scanf("%d",&n)&&n) { etot=0; memset(head,-1,sizeof(head)); memset(mat,-1,sizeof(mat)); scanf
("%d%d",&m,&k); for (int i=0;i<k;++i) { int waste,u,v; scanf("%d%d%d",&waste,&u,&v); adde(u,n+v); } int max_match=0; for (int i=1;i<=n;++i) { memset(vis,false,sizeof(vis)); if (dfs(i)) ++max_match; } printf("%d\n",max_match); } return 0; }