【dinic最大流 二进制缩点】M - Escape HDU - 3605

Escape

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


 

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

#include <bits/stdc++.h>
using namespace std;

const int inf = 0x7fffffff;
const int mn = 2000, mm = 40000;

int sum[mn];
int edge;
int fr[mn];
int lv[mn];
struct node
{
	int to, val, nx, fan;
} e[mm];

void init()
{
	edge = 0;
	memset(fr, -1, sizeof fr);
	memset(sum, 0, sizeof sum);
}

void addedge(int u, int v, int w)
{
	edge++;
	e[edge].to = v, e[edge].val = w, e[edge].nx = fr[u], e[edge].fan = edge + 1;
	fr[u] = edge;
	edge++;
	e[edge].to = u, e[edge].val = 0, e[edge].nx = fr[v], e[edge].fan = edge - 1;
	fr[v] = edge;
}

void bfs(int s)
{
	memset(lv, 0, sizeof lv);
	lv[s] = 1;
	queue<int>q;
	q.push(s);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		for (int i = fr[t]; i != -1; i = e[i].nx)
		{
			if (e[i].val > 0 && !lv[e[i].to])
			{
				lv[e[i].to] = lv[t] + 1;
				q.push(e[i].to);
			}
		}
	}
}

int dfs(int s, int t, int f)
{
	if (s == t)
		return f;
    for (int i = fr[s]; i != -1; i = e[i].nx)
	{
		if (e[i].val > 0 && lv[e[i].to] > lv[s])
		{
			int d = dfs(e[i].to, t, min(f, e[i].val));
			if (d > 0)
			{
				e[i].val -= d;
				e[e[i].fan].val += d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow(int s, int t)
{
	int flow = 0;
	while (1)
	{
		bfs(s);
		if (!lv[t])
			break;
		int f = -1;
        while ((f = dfs(s, t, inf)) > 0)
			flow += f;
	}
	return flow;
}

int main()
{
	//freopen("D:\\in.txt", "r", stdin);

	int n, m;
	int st = 0, ed = 1500;
	while (~scanf("%d %d", &n, &m))
	{
		init();

		/// 二进制缩点
		/// 10w个人 TLE 10个行星 (1 << 10)种情况
        for (int i = 1; i <= n; i++)
        {
            int id = 0;
            for (int j = 0; j < m; j++)
            {
                int x;
                scanf("%d", &x);
                if (x == 1)
                    id += (1 << j);
            }
            sum[id]++;
        }

        for (int i = 1; i < (1 << m); i++)
        {
            if (sum[i]) // 有人是第i种情况
            {
                addedge(10 + i, ed, sum[i]);

                int temp = i;
                for (int j = 0; j < m; j++)
                    if (temp >> j & 1)
                        addedge(j + 1, 10 + i, sum[i]);
            }
        }

        for (int i = 1; i <= m; i++)
        {
            int x;
            scanf("%d", &x);
            addedge(0, i, x);
        }

		if (n == max_flow(st, ed))
			printf("YES\n");
		else
			printf("NO\n");
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/81392987