Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 7831 Accepted Submission(s): 1702
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
//二進制壓縮加dinic
//因為最多十個星球,所有人的狀态可以分為2的10次方種,即1024種。統計相同狀态人的個數,然後建圖
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int N = 1500;
const int INF = 0x3f3f3f3f;
struct edge
{
int to, cap, rev;
};
vector <edge> G[N];
int level[N], iter[N];
void add_edge(int from, int to, int cap)
{
edge e;
e.to = to, e.cap = cap, e.rev = G[to].size();
G[from].push_back(e);
e.to = from, e.cap = 0, e.rev = G[from].size() - 1;
G[to].push_back(e);
}
void bfs(int s)
{
memset(level, -1, sizeof level);
queue <int> que;
level[s] = 0;
que.push(s);
while(! que.empty())
{
int v = que.front(); que.pop();
for(int i = 0; i < G[v].size(); i++)
{
edge &e = G[v][i];
if(e.cap > 0 && level[e.to] < 0)
{
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
int dfs(int v, int t, int f)
{
if(v == t) return f;
for(int &i = iter[v]; i < G[v].size(); i++)
{
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to])
{
int d = dfs(e.to, t, min(f, e.cap));
if(d > 0)
{
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t)
{
int flow = 0, f;
while(true)
{
bfs(s);
if(level[t] < 0) return flow;
memset(iter, 0, sizeof iter);
while(f = dfs(s, t, INF), f > 0)
flow += f;
}
}
int main()
{
int n, m, a;
int cnt[1500];
while(~ scanf("%d%d", &n, &m))
{
memset(cnt, 0, sizeof cnt);
int max1 = -1;
for(int i = 0; i < n; i++)
{
int tmp = 0;
for(int i = 0; i < m; i++)
{
scanf("%d", &a);
tmp = tmp * 2 + a;
}
max1 = max(max1, tmp);
cnt[tmp]++; //二進制壓縮,統計人數
}
for(int i = 0; i <= max1; i++)
{
if(cnt[i] == 0) continue;
add_edge(0, i, cnt[i]); //把0作為超級源點,壓縮狀态為0時不會連接配接,是以不必擔心和超級源點沖突
}
int b = max1 + 1; //b為第一個星球
for(int i = 0; i <= max1; i++)
{
if(cnt[i] == 0) continue;
int k = 0;
for(int j = m-1; j >= 0; j--)
{
if( ((i >> j) & 1) == 1)
add_edge(i, b + k, cnt[i]);
k++;
}
}
for(int i = 0; i < m; i++)
{
scanf("%d", &a);
add_edge(b + i, b + m, a); //b+m為超級彙點,連接配接星球和超級彙點
}
if(max_flow(0, b + m) >= n) printf("YES\n");
else printf("NO\n");
for(int i = 0; i <= b+m; i++)
G[i].clear();
}
return 0;
}