首页 > ACM题库 > HDU-杭电 > hdu 4322-candy-图-[解题报告]hoj
2015
05-23

hdu 4322-candy-图-[解题报告]hoj

http://acm.hdu.edu.cn/showproblem.php?pid=4322

Problem Description
There are N candies and M kids, the teacher will give this N candies to the M kids. The i-th kids for the j-th candy has a preference for like[i][j], if he like the sugar, like[i][j] = 1, otherwise like[i][j] = 0. If the i-th kids get the candy which he like
he will get K glad value. If he or she do not like it. He will get only one glad value. We know that the i-th kids will feel happy if he the sum of glad values is equal or greater than B[i]. Can you tell me whether reasonable allocate this N candies, make
every kid feel happy.
 


Input
The Input consists of several cases .The first line contains a single integer t .the number of test cases.
For each case starts with a line containing three integers N, M, K (1<=N<=13, 1<=M<=13, 2<=K<=10)
The next line contains M numbers which is B[i](0<=B[i]<=1000). Separated by a single space.
Then there are M*N like[i][j] , if the i-th kids like the j-th sugar like[i][j]=1 ,or like[i][j]=0.
 


Output
If there have a reasonable allocate make every kid feel happy, output "YES", or "NO".
 


Sample Input
2 3 2 2 2 2 0 0 0 0 0 1 3 2 2 2 2 0 0 0 0 0 0
 


Sample Output
Case #1: YES Case #2: NO
Hint
Give the first and second candy to the first kid. Give the third candy to the second kid. This allocate make all kids happy.
/**
hdu 4322  最大费用最大流

题目大意:(题解转)
        like[i][j]表示第i个孩子喜欢第j个糖果(总共m个孩子,n个糖)。 如果孩子拿到他喜欢的糖果,那么他将会增加k个欢乐值;
        拿到不喜欢的,增加1。 如果孩子i的欢乐值大于B[i],那么他才是开心的。能否有一种分配方案,让所有孩子都开心。
解题思路:
          首先声明,由于被小孩子不喜欢的糖果的对小孩产生的效力是一样的,所以我们在网络流的时候先不考虑。
          1 - 源点0到1~N个糖果,容量为1,费用为0
          2 - 根据like数组,like[i][j] == 1时在糖果j和人N+i之间建立有一条边,容量为1,费用为0
          3*- 根据b[i]和K的值建立小孩和汇点之间的边:
          如果b[i] 是 K 的倍数, 说明花费b[i] / K个喜欢的糖果可以达到b[i],建立一条边,费用为K,容量为b[i] / K;
          否则,将这条边拆为两部分,第一部分是b[i] / K的部分,第二部分根据b[i] % K的部分。(如果b[i] % k == 0,说明b[i]是k的倍数;
          若b[i] % k == 1, 特殊糖果和一般糖果价值一样,没必要当做特殊糖果处理)
          建好图后,求最大费用最大流(只需将费用改为负的,然后套最小费用最大流即可).。
          得出特殊糖果匹配b[i]的最大值。看剩余的普通糖果是否满足缺少的b[i]。
*/
#include <iostream>
#include <string.h>
#include <algorithm>
#include <cstdio>

using namespace std;

const int oo=1e9;//无穷大
const int maxm=1111;//边的最大数量,为原图的两倍
const int maxn=42;//点的最大数量

int node,src,dest,edge;//node节点数,src源点,dest汇点,edge边数
int head[maxn],p[maxn],dis[maxn],q[maxn],vis[maxn];//head链表头,p记录可行流上节点对应的反向边,dis计算距离
int n,m,k,dist[maxn],lik[maxn][maxn],Flow;

struct edgenode
{
    int to;//边的指向
    int flow;//边的容量
    int cost;//边的费用
    int next;//链表的下一条边
} edges[maxm];

void prepare(int _node,int _src,int _dest);
void addedge(int u,int v,int f,int c);
bool spfa();

inline int min(int a,int b)
{
    return a<b?a:b;
}

inline void prepare(int _node,int _src,int _dest)
{
    node=_node;
    src=_src;
    dest=_dest;
    for (int i=0; i<node; i++)
    {
        head[i]=-1;
        vis[i]=false;
    }
    edge=0;
}

void addedge(int u,int v,int f,int c)
{
    edges[edge].flow=f;
    edges[edge].cost=c;
    edges[edge].to=v;
    edges[edge].next=head[u];
    head[u]=edge++;
    edges[edge].flow=0;
    edges[edge].cost=-c;
    edges[edge].to=u;
    edges[edge].next=head[v];
    head[v]=edge++;
}

bool spfa()
{
    int i,u,v,l,r=0,tmp;
    for (i=0; i<node; i++) dis[i]=oo;
    dis[q[r++]=src]=0;
    p[src]=p[dest]=-1;
    for (l=0; l!=r; ((++l>=maxn)?l=0:1))
    {
        for (i=head[u=q[l]],vis[u]=false; i!=-1; i=edges[i].next)
        {
            if (edges[i].flow&&dis[v=edges[i].to]>(tmp=dis[u]+edges[i].cost))
            {
                dis[v]=tmp;
                p[v]=i^1;
                if (vis[v]) continue;
                vis[q[r++]=v]=true;
                if (r>=maxn) r=0;
            }
        }
    }
    return p[dest]>=0;
}

int spfaflow()
{
    int i,ret=0,delta;
    while (spfa())
    {
        //按记录原路返回求流量

        for (i=p[dest],delta=oo; i>=0; i=p[edges[i].to])
        {
            delta=min(delta,edges[i^1].flow);
        }
        for (int i=p[dest]; i>=0; i=p[edges[i].to])
        {
            edges[i].flow+=delta;
            edges[i^1].flow-=delta;
        }
        ret+=delta*dis[dest];
        Flow+=delta;
    }
    return ret;
}

int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        int sum=0;
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&dist[i]);
            sum+=dist[i];
        }
        prepare(n+m+2,0,n+m+1);
        for(int i=1; i<=n; i++)
        {
            addedge(src,i,1,0);
        }
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
            {
                int u;
                scanf("%d",&u);
                if(u==1)
                {
                    addedge(j,i+n,1,0);
                }
            }
        }
        for(int i=1;i<=m;i++)
        {
            addedge(i+n,dest,dist[i]/k,-k);
            if(dist[i]%k>1)
            {
                addedge(i+n,dest,1,-dist[i]%k);
            }
        }
        printf("Case #%d: ",++tt);
        Flow=0;
        int cost=spfaflow();
        if(sum+cost<=n-Flow)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。