牛客多校 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.
费用流
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int MAXM = 100010;
const int INF  = 0x3f3f3f3f;
struct Edge
{
    int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int pre[MAXN];
int dist[MAXN];
bool vis[MAXN];
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w, int c)
{
  //  cout<<u<<" "<<v<<" "<<w<<" "<<c<<endl;

    Edge E1 = {u, v, w, 0, c, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, -c, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
bool SPFA(int s, int t)
{
    queue<int> Q;
    memset(dist, INF, sizeof(dist));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dist[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)
        {
            Edge E = edge[i];
            if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + E.cost;
                pre[E.to] = i;
                if(!vis[E.to])
                {
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
    }
    return pre[t] != -1;
}
void MCMF(int s, int t, int &cost, int &flow)
{
    flow = 0;
    cost = 0;
    while(SPFA(s, t))
    {
        int Min = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            Edge E = edge[i];
            Min = min(Min, E.cap - E.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;
    }
}
struct domitary
{
    int mem[5]={0};
};

set<int> lz[500],xz[500];

int main()
{
    int n,i,j;
    domitary a[105],b[105];
    scanf("%d",&n);
    init();
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=4;j++)
        {
            scanf("%d",&a[i].mem[j]);
            lz[i].insert(a[i].mem[j]);
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=4;j++)
        {
            scanf("%d",&b[i].mem[j]);
            xz[i].insert(b[i].mem[j]);
        }
    }
    int k,l;
    set<int> cc;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            cc.clear();
            //cout<<"gg"<<endl;
            set_intersection(xz[i].begin(),xz[i].end(),
                             lz[j].begin(),lz[j].end(),
                             inserter(cc,cc.begin())
                             );
              addEdge(i,j+n,1,4-cc.size());
        }
    }
    int st=0;
    for(i=1;i<=n;i++)
    {
        addEdge(st,i,1,0);
    }
    int sink=2*n+1;
    for(i=n+1;i<=2*n;i++)
    {
        addEdge(i,sink,1,0);
    }
    int cos,fl;
    MCMF(st,sink,cos,fl);
    printf("%d\n",cos);
    return 0;
}

  

posted @ 2018-08-02 20:45  行远山  阅读(144)  评论(0编辑  收藏  举报