连着做两道最小点集覆盖的题,直接拿上一题代码改的,都没改几句。。
/* * hdu1150/win.cpp * Created on: 2012-8-14 * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXN = 210; int N, M, mymatch[MAXN]; bool visited[MAXN], mymap[MAXN][MAXN]; bool buildgraph() { int t, K, a, b; scanf("%d", &N); if(N == 0) { return false; } scanf("%d%d", &M, &K); memset(mymap, false, sizeof(mymap)); for(int i = 0; i < K; i++) { scanf("%d%d%d", &t, &a, &b); if(a != 0 && b != 0) { mymap[a - 1][b - 1] = true; } } return true; } bool dfs(int k) { int t; for (int i = 0; i < M; i++) { if (mymap[k][i] && !visited[i]) { visited[i] = true; t = mymatch[i]; mymatch[i] = k; if (t == -1 || dfs(t)) { return true; } mymatch[i] = t; } } return false; } int hungary () { memset(mymatch, -1, sizeof(mymatch)); int ans = 0; for (int i = 0; i < N; i++) { memset(visited, false, sizeof(visited)); if (dfs(i)) { ans++; } } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif while(buildgraph()) { printf("%d\n", hungary()); } return 0; }