hdu 1150(最小点覆盖)

来源:互联网 发布:淘宝 快递信息录入 编辑:程序博客网 时间:2024/05/20 17:23

传送门
二分图最大匹配=最小点覆盖
题解:二分图左边点集为机器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;}
阅读全文
0 0
原创粉丝点击