牛客网暑期多校第五场E room(费用流)
题意:有n个宿舍,每个宿舍住4个人,给定去年n个宿舍的人员构成,今年n个4人组合的情况,求最少几个人需要搬寝室。
思路:转化为最小费用最大流解决的二分图问题,对每个去年的宿舍,向每个今年的组合连一条边,权值为1费用为需要搬的人数(4-相同的人数),源点到去年各点,今年各点到汇点,都连一条权值为1费用为0的最大流,跑一次费用流即可。
注意点数和边数,不然会段错误(RE)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<queue> using namespace std; const int MAXN = 205; const int MAXM = 40005; const int INF = 0x3f3f3f3f; struct Edge { int to,next,cap,flow,cost; } edge[MAXM]; int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N;//节点总个数,节点编号从0~N-1 void init(int n) { N = n; tol = 0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int cap,int cost) { edge[tol].to = v; edge[tol].cap = cap; edge[tol].cost = cost; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = 0; edge[tol].cost = -cost; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++; } bool spfa(int s,int t) { queue<int>q; for(int i = 0; i < N; i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[s] = 0; vis[s] = true; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ) { dis[v] = dis[u] + edge[i].cost; pre[v] = i; if(!vis[v]) { vis[v] = true; q.push(v); } } } } if(pre[t] == -1)return false; else return true; } //返回的是最大流,cost存的是最小费用 int minCostMaxflow(int s,int t,int &cost) { int flow = 0; cost = 0; while(spfa(s,t)) { int Min = INF; for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { if(Min > edge[i].cap - edge[i].flow) Min = edge[i].cap - edge[i].flow; } for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { edge[i].flow += Min; edge[i^1].flow -= Min; cost += edge[i].cost * Min; } flow += Min; } return flow; } int n; struct node{ int a,b,c,d; }r[205]; int cou(int x,int y) { int res=4; res-=(r[x].a==r[y].a||r[x].a==r[y].b||r[x].a==r[y].c||r[x].a==r[y].d); res-=(r[x].b==r[y].a||r[x].b==r[y].b||r[x].b==r[y].c||r[x].b==r[y].d); res-=(r[x].c==r[y].a||r[x].c==r[y].b||r[x].c==r[y].c||r[x].c==r[y].d); res-=(r[x].d==r[y].a||r[x].d==r[y].b||r[x].d==r[y].c||r[x].d==r[y].d); return res; } int main() { scanf("%d",&n); for (int i=1;i<=2*n;i++) { scanf("%d%d%d%d",&r[i].a,&r[i].b,&r[i].c,&r[i].d); } init(2*n+2); for (int i=1;i<=n;i++) { addedge(0,i,1,0); addedge(n+i,2*n+1,1,0); for (int j=n+1;j<=2*n;j++) { addedge(i,j,1,cou(i,j)); } } int ans; minCostMaxflow(0,2*n+1,ans); printf("%d\n",ans); return 0; }