題目背景
第二次世界大戰時期..
題目描述
英國皇家空軍從淪陷國徵募了大量外籍飛行員。由皇家空軍派出的每一架飛機都需要配備在航行技能和語言上能互相配合的2 名飛行員,其中1 名是英國飛行員,另1名是外籍飛行員。在衆多的飛行員中,每一名外籍飛行員都可以與其他若干名英國飛行員很好地配合。如何選擇配對飛行的飛行員才能使一次派出最多的飛機。對於給定的外籍飛行員與英國飛行員的配合情況,試設計一個算法找出最佳飛行員配對方案,使皇家空軍一次能派出最多的飛機。
對於給定的外籍飛行員與英國飛行員的配合情況,編程找出一個最佳飛行員配對方案,使皇家空軍一次能派出最多的飛機。
輸入格式
第 1 行有 2 個正整數 m 和 n。n 是皇家空軍的飛行員總數(n<100);m 是外籍飛行員數(m<=n)。外籍飛行員編號爲 1~m;英國飛行員編號爲 m+1~n。
接下來每行有 2 個正整數 i 和 j,表示外籍飛行員 i 可以和英國飛行員 j 配合。最後以 2個-1 結束。
輸出格式
第 1 行是最佳飛行員配對方案一次能派出的最多的飛機數 M。接下來 M 行是最佳飛行員配對方案。每行有 2個正整數 i 和 j,表示在最佳飛行員配對方案中,飛行員 i 和飛行員 j 配對。如果所求的最佳飛行員配對方案不存在,則輸出‘No Solution!’。
輸入輸出樣例
輸入 #1複製
5 10
1 7
1 8
2 6
2 9
2 10
3 7
3 8
4 7
4 8
5 10
-1 -1
輸出 #1複製
4
1 7
2 9
3 8
5 10
dinic加輸出路徑板子
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const ll MAXN = 1000;
const ll maxm = 100000;
struct Edge{
ll from, to, cap, flow;
Edge(){
}
Edge(ll from, ll to, ll cap, ll flow):from(from), to(to), cap(cap), flow(flow){}
};
struct Edge1
{
ll from, to; ll dist; //起點,終點,距離
Edge1(ll from, ll to, ll dist):from(from), to(to), dist(dist) {}
};
struct Dinic{
ll n, m, s, t;
vector<Edge>edges;
vector<ll>G[MAXN];
ll d[MAXN];
ll cur[MAXN];
ll vis[MAXN];
void init(ll n, ll s, ll t)
{
this->n = n;this->s = s;this->t = t;
edges.clear();
for(ll i = 0;i <= n;++i) G[i].clear();
}
void add_edge(ll from, ll to, ll cap)
{
edges.push_back( Edge(from, to, cap, 0) );
edges.push_back( Edge(to, from, 0, 0) );
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bfs(){
memset(vis, 0, sizeof(vis));
queue<ll>Q;
Q.push(s);
d[s] = 0;
vis[s] = true;
while(!Q.empty())
{
ll x = Q.front();
Q.pop();
for(ll i = 0;i < G[x].size();++i)
{
Edge& e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = true;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
ll dfs(ll x,ll a)
{
if(x == t || a == 0)return a;
ll flow = 0, f;
for(ll& i = cur[x];i < G[x].size();++i)
{
Edge& e = edges[G[x][i]];
if(d[x] + 1 == d[e.to] && (f = dfs( e.to, min(a, e.cap-e.flow)))>0)
{
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0)break;
}
}
return flow;
}
ll maxflow()
{
ll flow = 0;
while(bfs())
{
memset(cur, 0, sizeof(cur));
flow += dfs(s,inf);
}
return flow;
}
}gao;//劉汝佳網絡流dinic板子
int main()
{
int m, n;
scanf("%d%d", &m, &n);
gao.init(n+2, n+1, n+2);
int x, y;
for(int i = 0;;++i)
{
scanf("%d%d", &x, &y);
if(x == -1 && y == -1)
{
break;
}
gao.add_edge(x, y, 1);
}
for(int i = 1;i <= m;++i)
{
gao.add_edge(n+1, i, 1);
}
for(int i = m+1;i <= n;++i)
{
gao.add_edge(i, n+2, 1);
}
int d = gao.maxflow();
if(d)
{
printf("%lld\n", d);
for(int i = 0;i < gao.edges.size();++i)
{
Edge& e = gao.edges[i];
if(e.from != n+1 && e.to != n+2 && e.flow == 1)
{
printf("%d %d\n", e.from, e.to);
}
}
}
else{
printf("No Solution!\n");
}
return 0;
}