E、room (费用流)

Nowcoder University has 4n students and n dormitories ( Four students per dormitory). Students numbered from 1 to 4n. And in the first year, the i-th dormitory 's students are (x1[i],x2[i],x3[i],x4[i]), now in the second year, Students need to decide who to live with. In the second year, you get n tables such as (y1,y2,y3,y4) denote these four students want to live together. Now you need to decide which dormitory everyone lives in to minimize the number of students who change dormitory. 
 
输入描述: The first line has one integer n. 
 
Then there are n lines, each line has four integers (x1,x2,x3,x4) denote these four students live together in the first year 
 
Then there are n lines, each line has four integers (y1,y2,y3,y4) denote these four students want to live together in the second year 
 
输出描述: Output the least number of students need to change dormitory. 
 
备注: 1<=n<=100 
 
1<=x1,x2,x3,x4,y1,y2,y3,y4<=4n 
 
It's guaranteed that no student will live in more than one dormitories. 
 
示例 1 输入 2 1 2 3 4 5 6 7 8 4 6 7 8 1 2 3 5 输出 2 
 
说明 Just swap 4 and 5

 

第一年的向第二年的连边,流量为1,费用为 - 第一年宿舍与第二年宿舍相同人数。

S与第一年连边,流量为1,费用为0.  第二年向T连边,流量为1,费用为0.

这样的话,跑一边费用流,求出的不用移动的学生的最大人数(负的),然后用总数相加即可。

 

代码:


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define clear(A, X) memset(A, X, sizeof A)
#define min(A, B) ((A) < (B) ? (A) : (B))
using namespace std;

const int maxE = 2000000;
const int maxN = 256;
const int oo = 0x3f3f3f3f;

struct Edge{
	int v, c, w, n;
	Edge(){}
	Edge(int V, int C, int W, int N) : v(V), c(C), w(W), n(N){}
};

struct Node{
	int l, r, w;
};
struct poin
{
    int x,y;
}x1[300],x2[300];
Edge edge[maxE];
Node sche[maxN];
int adj[maxN], cntE;
int Q[maxE], head, tail;
int d[maxN], inq[maxN], cur[maxN], f[maxN];
int cost, flow, s, t;
int N, M, K;

void addedge(int u, int v, int c, int w){
	edge[cntE] = Edge(v, c,  w, adj[u]); adj[u] = cntE++;
	edge[cntE] = Edge(u, 0, -w, adj[v]); adj[v] = cntE++;
}

int spfa(){
	f[s] = oo;
	clear(d, oo);
	clear(inq, 0);
	cur[s] = -1;
	d[s] = 0;
	head = tail = 0;
	Q[tail++] = s;
	inq[s] = 1;
	while(head != tail){
		int u = Q[head++];
		inq[u] = 0;
		for(int i = adj[u]; ~i; i = edge[i].n){
			int v = edge[i].v, c = edge[i].c, w = edge[i].w;
			if(c && d[v] > d[u] + w){
				d[v] = d[u] + w;
				f[v] = min(f[u], c);
				cur[v] = i;
				if(!inq[v]){
					Q[tail++] = v;
					inq[v] = 1;
				}
			}
		}
	}
	if(d[t] == oo) return 0;
	flow += f[t];
	cost += d[t] * f[t];
	for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){
		edge[i].c -= f[t];
		edge[i ^ 1].c += f[t];
	}
	return 1;
}

int MCMF(){
	flow = cost = 0;
	while(spfa());
	return cost;
}

void init(){
	clear(adj, -1);
	cntE = 0;
}

int a[110][4];
int b[110][4];
int get(int x, int y) {
    int res = 0;
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    if (a[x][i] == b[y][j]) res++;
    return res;
}
int S,T;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)for(int j=0;j<4;j++)scanf("%d",&a[i][j]);
    for(int i=1;i<=n;i++)for(int j=0;j<4;j++)scanf("%d",&b[i][j]);
    s=0;t=2*n+1;
    init();
    for(int i=1;i<=n;i++)
    {
        addedge(s, i, 1, 0);
        addedge(n + i, t, 1, 0);
    }
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    addedge(i, n + j, 1, -get(i, j));
    printf("%d\n",4*n+MCMF());
    return 0;
}
/*
2
1 2 3 4
5 6 7 8
4 6 7 8
1 2 3 5
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值