牛客网暑期ACM多校训练营(第五场)E room (费用流)

链接:https://www.nowcoder.com/acm/contest/143/E
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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

输入

复制

2
1 2 3 4
5 6 7 8
4 6 7 8
1 2 3 5

输出

复制

2

说明

Just swap 4 and 5

备注:

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.

题目大意:给出了n个宿舍,每个宿舍有4个人,前n行给出的是第一年的住宿情况,

后n行给出的是第二年4个人想住在一起的情况,问最少需要多少人移动。

解题思路:建图,由于当前寝室的成员如果搬出去之后我们不管他们进入哪一个寝室,都会对答案做出贡献,

因此我们把宿舍看成一个整体,让宿舍之间连边,容量为1.花费为两个宿舍成员不同的个数。

这样的话,如果去跑费用流那么答案就是最大流为n时的最小费用。

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define sca(x) scanf("%d",&x)
#define pb(x) push_back(x)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x3f3f3f3f
#define LL long long
#define N 300
#define MAXN 40005
#define inf 0x3f3f3f3f

int S,T;
int a[105][5],b[105][5];

struct edg
{
    int to,w,c,nt;
}g[MAXN];

int tot;
int head[N];
void addedg(int u,int v,int w,int c)
{
    g[tot].to=v;
    g[tot].w=w;
    g[tot].c=c;
    g[tot].nt=head[u];
    head[u]=tot++;

    g[tot].to=u;
    g[tot].w=0;
    g[tot].c=-c;
    g[tot].nt=head[v];
    head[v]=tot++;

}

int C(int x,int y)
{
    int ans=0;
    rep(i,1,4)
    {
        rep(j,1,4)
        {
            if(a[x][i]==b[y][j])
            {
                ans++;
                break;
            }
        }
    }
    return ans;
}

struct node
{
    int id,v;
}pre[N];

int vis[N],dist[N];
bool spfa(int s,int t)
{
    memset(vis,false,sizeof(vis));
    memset(dist,inf,sizeof(dist));
    queue<int>q;
    q.push(s);
    dist[s]=0;
    vis[s]=true;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=g[i].nt)
        {
            int to=g[i].to;
            if(dist[to]>dist[u]+g[i].c&&g[i].w>0)
            {
                dist[to]=dist[u]+g[i].c;
                pre[to].v=u;
                pre[to].id=i;
                if(!vis[to])
                {
                    vis[to]=true;
                    q.push(to);
                }
            }
        }
    }
    return dist[t]!=inf;
}

void solve(int s,int t)
{
    int ans=0;
    int mini;
    while(spfa(s,t))
    {
        mini=inf;
        for(int i=t;i!=s;i=pre[i].v)
        {
            mini=min(mini,g[pre[i].id].w);
        }
        for(int i=t;i!=s;i=pre[i].v)
        {
            g[pre[i].id].w-=mini;
            g[pre[i].id^1].w+=mini;
        }
        ans+=dist[t]*mini;
    }
    printf("%d\n",ans);
}

int main()
{
    int n;
    sca(n);
    S=0,T=n*2+1;
    memset(head,-1,sizeof(head));
    rep(i,1,n)
    {
        scanf("%d%d%d%d",&a[i][1],&a[i][2],&a[i][3],&a[i][4]);
        addedg(S,i,1,0);
    }
    rep(i,1,n)
    {
        scanf("%d%d%d%d",&b[i][1],&b[i][2],&b[i][3],&b[i][4]);
        addedg(n+i,T,1,0);
    }
    rep(i,1,n)
    {
        rep(j,1,n)
        {
            addedg(i,j+n,1,4-C(i,j));
        }
    }
    solve(S,T);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40894017/article/details/83098165