题意:
一共有n个宿舍,每个宿舍有4个人。给出第一年的人员分布和第二年的人员分布,问至少有多少人需要移动。
题解:
对于第一年的每个宿舍,向今年的每种组合连边。流量为1,费用为(4 - 组合中已在该宿舍的人数)。
最后跑一边费用流。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 205; const int INF = 0x3f3f3f3f; int n, k; int x[405], y[405]; int g[105][5]; typedef pair<int, int> P; struct edge { int to, cap, cost, rev; }; int V; vector<edge> G[N]; int h[N]; int dist[N]; int prevv[N], preve[N]; void add_edge(int from, int to, int cap, int cost) { G[from].push_back((edge){to, cap, cost, (int)G[to].size()}); G[to].push_back((edge){from, 0, -cost, (int)G[from].size()-1}); } int min_cost_flow(int s, int t, int f) { int res = 0; fill(h, h + V, 0); while(f > 0) { priority_queue<P, vector<P>, greater<P> > que; fill(dist, dist + V, INF); dist[s] = 0; que.push(P(0, s)); while(!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if(dist[v] < p.first) continue; int len = G[v].size(); for(int i = 0; i < len; i++) { edge &e = G[v][i]; if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push(P(dist[e.to], e.to)); } } } if(dist[t] == INF) return -1; for(int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for(int v = t; v != s; v = prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap); f -= d; res += d*h[t]; for(int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) for(int j = 1; j <= 4; j++) { scanf("%d", &k); x[k] = i; y[k] = j; } for(int i = 1; i <= n; i++) for(int j = 1; j <= 4; j++) { scanf("%d", &k); g[x[k]][y[k]] = i; } for(int i = 1; i <= n; i++) add_edge(0, i, 1, 0); for(int i = n+1; i <= 2*n; i++) add_edge(i, 2*n+1, 1, 0); for(int i = 1; i <= n; i++) { for(int j = n+1; j <= 2*n; j++) { int cnt = 4; for(int k = 1; k <= 4; k++) if(g[i][k] == j-n) cnt--; add_edge(i, j, 1, cnt); } } V = 2*n+2; printf("%d ", min_cost_flow(0, 2*n+1, n)); }