2018、牛客网暑期ACM多校训练营(第五场)E--room

题意:有n间教室,每间教室可以坐四个人,给出新的一年四个人的组合意愿,求最少有几人搬宿舍,能满足所有人的意愿。

思路:长得这么像贪心的一个题!!竟然是个网络流!!今天终于A出来了。自己傻了!!傻了!!,一直调不出来是赋初值语句放错了位置!!!!!然而其实往网络流上套是真的不难,,,有模板,就是改改输入就可以了!!

感想:现在做题,真的应该锻炼自己的判题能力了,这个题在比赛的时候真的靠了太久了,还是错的方法,,完全没想到是个网络流。。心痛!!

代码:

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=200+10;
int head[maxn],aa[250][250],fre[250][250];
struct Edge
{
    int from,to,cap,flow,cost,next;
    Edge(){}
Edge(int f,int t,int c,int fl,int co,int ne):
from(f),to(t),cap(c),flow(fl),cost(co),next(ne){}
};
int same(int x,int y)
{
   // cout<<x<<" ! "<<y<<endl;
    int s=0;
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            if(aa[x][i]==fre[y][j]) {s++;break;}
        }
        //cout<<i<<" "<<s<<endl;
    }
    return s;
}
struct MCMF
{
    int n,m,s,t;
    vector<Edge> edges;
    bool inq[maxn];   //是否在队列
    int d[maxn];     //Bellman_ford单源最短路径
    int p[maxn];     //p[i]表从s到i的最小费用路径上的最后一条弧编号
    int a[maxn];     //a[i]表示从s到i的最小残量
    //初始化
    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
    }
    //添加一条有向边
    void AddEdge(int from,int to,int cap,int cost)
    {
        edges.push_back(Edge(from,to,cap,0,cost,head[from]));
        edges.push_back(Edge(to,from,0,0,-cost,head[to]));
        m=edges.size();
        head[from]=m-2;
        head[to]=m-1;
    }
    //求一次增广路
    bool BellmanFord(int &flow, int &cost)
    {
        for(int i=0;i<=n;++i) {d[i]=INF;}
        memset(inq,0,sizeof(inq));
        d[s]=0, a[s]=INF, inq[s]=true, p[s]=0;
        queue<int> Q;
        Q.push(s);
        while(!Q.empty())
        {
            int u=Q.front(); Q.pop();
            inq[u]=false;
           //cout<<u<<" ! "<<head[u]<<endl;

            for(int i=head[u];i!=-1;i=edges[i].next)
            {//cout<<i<<"!!"<<edges[i].next<<endl;
                Edge &e=edges[i];
                //cout<<u<<" "<<e.to<<" "<<e.cap<<" "<<e.flow<<" "<<d[e.to]<<endl;
                if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
                {
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=i;
                    a[e.to]= min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){ Q.push(e.to); inq[e.to]=true; }
                }
            }
            //cout<<Q.size()<<endl;
        }
       // cout<<t<<" "<<d[t]<<endl;
        if(d[t] ==INF) return false;
        flow+=a[t];
        cost+=a[t]*d[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
            u=edges[p[u]].from;
        }
        return true;
    }
    //求出最小费用最大流
    int Min_cost(int all)
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        if(flow<all) cost=-1;
        return cost;
    }
}MM;
int main()
{
    int x,y,cc,q,zz,n;
    scanf("%d",&n);
    MM.init(n+n+1,0,n+n+1);
    memset(head,-1,sizeof(head));


    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=4;j++)
        scanf("%d",&fre[i][j]);
        MM.AddEdge(0,i,1,0);
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=4;j++)
        scanf("%d",&aa[i][j]);
        MM.AddEdge(n+i,n+n+1,1,0);
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            MM.AddEdge(i,n+j,1,4-same(i,j));
        }
    }
       printf("%d\n",MM.Min_cost(n));

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值