http://www.byvoid.com/blog/hungary/
回忆了一下图论知识,用byvoid大牛的例子自己算了一下才明白..
如果还没有明白,就看这个
http://www.matrix67.com/blog/archives/39
ps 元学习就是比看图论书快啊..
真的很漂亮啊,简单粗暴美丽
poj 1274
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1100;
int nn, mm;
int adj[maxn][maxn];
int vis[maxn], mat[maxn];
bool aupath(int u);
int hungary();
void input();
int main() {
while (cin>>nn>>mm) {
input();
cout<<hungary()<<endl;
}
return 0;
}
void input() {
for (int i=1; i<=nn; i++) {
cin>>adj[i][0];
for (int k=1; k<=adj[i][0]; k++) {
cin>>adj[i][k];
}
}
}
int hungary() {
memset(mat, 0, sizeof(mat));
int match = 0;
for (int i=1; i<=nn; i++) {
memset(vis, 0, sizeof(vis));
if (aupath(i)) {
match++;
}
}
return match;
}
bool aupath(int u) {
for (int i=1; i<=adj[u][0]; i++) {
int v = adj[u][i];
if (!vis[v]) {
vis[v] = 1;
if (mat[v] == 0 || aupath(mat[v])) {
mat[v] = u;
return true;
}
}
}
return false;
}