标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14375 | Accepted: 6135 |
Description
Input
Output
Sample Input
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
Sample Output
3
【分析】本题要求二部图的最小点覆盖集问题,即求最小的顶点集合,覆盖住所有的点。转换成求二部图的最大匹配问题,因为:二部图的点覆盖数==匹配数。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #include<functional> #define mod 1000000007 #define inf 0x3f3f3f3f #define pi acos(-1.0) using namespace std; typedef long long ll; const int N=105; const int M=15005; int nx,ny; int job; int edg[N][N]; int ans=0; int visx[N],visy[N]; int cx[N],cy[N]; int dfs(int u) { visx[u]=1; for(int v=1;v<=ny;v++){ if(edg[u][v]&&!visy[v]){ visy[v]=1; if(!cy[v]||dfs(cy[v])){ cx[u]=v;cy[v]=u; return 1; } } } return 0; } int solve() { memset(cx,0,sizeof(cx));memset(cy,0,sizeof(cy)); for(int i=1;i<=nx;i++){ if(!cx[i]){ memset(visx,0,sizeof(visx));memset(visy,0,sizeof(visy)); ans+=dfs(i); } } } int main() { int x,y,m; scanf("%d%d%d",&nx,&ny,&job); memset(edg,0,sizeof(edg)); for(int i=0;i<job;i++){ scanf("%d%d%d",&m,&x,&y); edg[x][y]=1; } solve(); printf("%d\n",ans); return 0; }
POJ1325Machine Schedule(匈牙利算法)
标签:
原文地址:http://www.cnblogs.com/jianrenfang/p/5791269.html