51Nod-2006-飛行員配對(二分圖最大匹配)
阿新 • • 發佈:2019-02-01
描述
題解
這個題的來源是網路流24題,貌似是一個十分不錯的網路流習題集,暑假抽空做做吧!
越學習,越發現自己是一個大大的菜雞,心痛啊~~~我網路流都不會……
這個題很明顯是一個二分圖的最大匹配問題(不要問我怎麼看出來的),所以既可以用匈牙利演算法解(程式碼 One),也可以使用構建網路流模型來解(程式碼 Two)。匈牙利的問題就不多說了,裸演算法,(0級題都是裸演算法模版題),而這裡如果用網路流模型來解,就涉及到了一個建圖的問題,首先我們先建立一個超級源點通向外籍飛行員,權值為
增廣路相關演算法真是有趣至極,今天看了好久,總算是有所入門了,待我暑假,網路流專題走起!!!
程式碼
One:
// 匈牙利演算法
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 105;
const int MAXE = 1e5 + 10;
struct edge
{
int v;
int nt;
};
int n, m;
int ans, tot;
int head[MAXN];
int match[MAXN];
bool flag[MAXN];
edge map[MAXE];
void add_edge(int x, int y)
{
tot++;
map[tot].v = y;
map[tot].nt = head[x];
head[x] = tot;
}
void init()
{
memset(head, 0, sizeof(head));
memset(match, 0 , sizeof(match));
ans = tot = 0;
}
bool dfs(int x)
{
for (int e = head[x]; e; e = map[e].nt)
{
int v = map[e].v;
if (!flag[v])
{
flag[v] = true;
if (match[v] == 0 || dfs(match[v]))
{
match[v] = x;
return true;
}
}
}
return false;
}
void hungary()
{
for (int i = 1; i <= m; i++)
{
memset(flag, false, sizeof(flag));
if (dfs(i))
{
ans++;
}
}
}
int main()
{
init();
scanf("%d%d", &m, &n);
int x, y;
while (scanf("%d%d", &x, &y), x != -1 && y != -1)
{
add_edge(x, y);
}
hungary();
printf("%d\n", ans);
return 0;
}
Two:
// 網路流模型
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int MAXN = 105;
const int MAXE = 1e5 + 10;
const int INF = 0x3f3f3f3f;
struct edge
{
int v;
int next, flow, op; // opposite
};
int n, m;
int ans, tot;
int st, ed; // 超級源點與超級匯點
int dis[MAXN];
int head[MAXN];
int path[MAXN];
bool vis[MAXN];
edge map[MAXE];
queue<int> que;
stack<int> sta;
void add_edge(int x, int y, int flow)
{
tot++;
map[tot].v = y;
map[tot].flow = flow;
map[tot].next = head[x];
head[x] = tot;
map[tot].op = tot + 1;
tot++;
map[tot].v = x;
map[tot].flow = 0;
map[tot].next = head[y];
head[y] = tot;
map[tot].op = tot - 1;
}
void init()
{
ans = tot = 0;
memset(head, 0, sizeof(head));
st = 0;
ed = n + 1;
for (int i = 1; i <= m; i++)
{
add_edge(st, i, 1);
}
for (int i = m + 1; i <= n; i++)
{
add_edge(i, ed, 1);
}
}
bool SPFA()
{
memset(vis, false, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
vis[st] = true;
dis[st] = 0;
while (!que.empty())
{
que.pop();
}
que.push(st);
while (!que.empty())
{
int u = que.front();
que.pop();
vis[u] = false;
for (int t = head[u]; t; t = map[t].next)
{
int v = map[t].v;
if (dis[u] + 1 < dis[v] && map[t].flow)
{
dis[v] = dis[u] + 1;
if (!vis[v])
{
vis[v] = true;
que.push(v);
}
}
}
}
if (dis[ed] < INF)
{
return true;
}
return false;
}
void Dinic()
{
while (!sta.empty())
{
sta.pop();
}
sta.push(st);
int x, last = 1;
while (last)
{
while (last != sta.size())
{
sta.pop();
}
x = sta.top();
if (x != ed)
{
int t = head[x];
while (t && (dis[x] + 1 != dis[map[t].v] || map[t].flow == 0))
{
t = map[t].next;
}
if (t == 0)
{
dis[x] = INF;
sta.pop();
last--;
}
else
{
path[last] = t;
sta.push(map[t].v);
last++;
}
}
else
{
int min_flow = INF;
for (int i = 1; i < last; i++)
{
if (map[path[i]].flow < min_flow)
{
min_flow = map[path[i]].flow;
}
}
ans += min_flow;
int tmp = 0;
for (int i = last - 1; i > 0; i--)
{
map[path[i]].flow -= min_flow;
map[map[path[i]].op].flow += min_flow;
if (map[path[i]].flow == 0)
{
tmp = i;
}
}
last = tmp;
}
}
}
void solve()
{
while (SPFA())
{
Dinic();
}
}
int main()
{
scanf("%d%d", &m, &n);
init();
int x, y;
while (scanf("%d%d", &x, &y), x != -1 && y != -1)
{
add_edge(x, y, 1);
}
solve();
if (ans == 0)
{
printf("No Solution!\n");
}
else
{
printf("%d\n", ans);
}
return 0;
}