HDU 3605 Escape 最大流 二进制并点建图

                                              Escape

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 12757    Accepted Submission(s): 3169


 

Problem Description

2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.

 

 

Input

More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000

 

 

Output

Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.

 

 

Sample Input

1 1
1
1

2 2
1 0
1 0
1 1

 

 

 

Sample Output

 
YES
NO

 

 

Source

2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU

 

因为m只有10,所以二进制将人分类最多2^10 = 1024种,然后建图跑最大流板子即可!

 

 

//Max_flow
//@2018/05/04 Friday
//SAP  O(n^2 * m)  O(m*3*2)
//by Tawn
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>
#include <cmath>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 100;
const int maxm = 1000000 + 10;
typedef pair<int,int> P;

int head[maxn];//链式前向星
int tot = 0,num;
int n,m;

struct edge
{
  int to;
  int c;
  int next;
  edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
 }es[maxm*2];//记录边 注意是2倍

void add_edge(int u, int v, int c)
{
    es[tot] = edge(v,c,head[u]);
    head[u] = tot++;
}


int SAP(int s, int t)
{
    int numh[maxn],h[maxn],ce[maxn],pre[maxn];
    //numh 记录gap优化的统计高度数量数组,h 距离标号数组,ce 当前弧,pre前驱数组
    int f, ans = 0, u, temp, neck, i; //初始化最大流为0
    memset(h,0,sizeof(h));
    memset(numh,0,sizeof(numh));
    memset(pre,-1,sizeof(pre));
    for(i = 0; i <= num; i++)  ce[i] = head[i];
    numh[0] = num;
    u = s;
    while(h[s] < num)
    {
        //寻找增广路
        if(u == t)
        {
            f = INF;
            for(i = s; i != t; i = es[ce[i]].to)
            {
                if(f > es[ce[i]].c)
                {
                    neck = i;
                    f = es[ce[i]].c;
                }
            }
            for(i = s; i != t; i = es[ce[i]].to)
            {
                temp = ce[i];
                es[temp].c -= f;
                es[temp^1].c += f;
            }
            ans += f;
            u = neck;
        }

        //寻找可行弧
        for(i = ce[u]; i != -1; i = es[i].next)
            if(es[i].c && h[u] == h[es[i].to] + 1)  break;

       //寻找增广路
        if(i != -1)
        {
            ce[u] = i;
            pre[es[i].to] = u;
            u = es[i].to;
        }
        else
        {
            if(!--numh[h[u]]) break; //gap optimization
            ce[u] = head[u];
            for(temp = num, i = head[u]; i != -1; i = es[i].next)
                if(es[i].c)  temp = min(temp, h[es[i].to]);

            h[u] = temp + 1;
            ++numh[h[u]];
            if(u != s) u = pre[u];//重标号并且从当前点前驱重新增广
        }

    }
    return ans;
}

int a[1050];

int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    memset(head,-1,sizeof(head));
    memset(a,0,sizeof(a));
    int s = pow(2,m);
    int t = s+m+1;
    for(int i = 0; i < n; i++)
      {
        int res = 0,temp;
        for(int j = 0; j < m; j++)
        {
          scanf("%d",&temp);
          res *= 2;
          res += temp;
        }
       a[res] ++;
      }
    //for(int i = 0; i < s; i++) cout << a[i] << " ";
    //cout << endl;
    for(int i = 0; i < s; i++)
     {
       if(a[i] != 0)
         {
           add_edge(s,i,a[i]);
           add_edge(i,s,0);
           int p = 1;
           for(int x = 1; x < s; x <<= 1,p++)  if(x & i) {add_edge(i,s+p,a[i]);add_edge(s+p,i,0); }
         }
     }
   for(int i = m; i >= 1; i--)
      {
        int p;
        scanf("%d",&p);
        add_edge(s+i,t,p);
        add_edge(t,s+i,0);
      }
    num = t + 1;
    int ans = SAP(s,t);
    if(ans == n) printf("YES\n");
    else  printf("NO\n");
//    cout << s << " " << t << " " << ans << endl;
  }
  return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值