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
题目大意 : 有N个人, 要逃往M个星球, 但是每个人都有自己喜欢和不喜欢的星球 (1 和 0),每个人只会逃往喜欢的星球, 如果所有人都能逃掉, 输出 YES, 否则 输出 NO
思路 : 一开始看以为是个板子题,提交TLE,仔细看了下数据发现事情没那么简单QAQ 这道题需要你状态压缩, 之前写过一道类似的题, 也是边太多导致超时, 写完当时那题后才知道如果两个点他们的来源相同, 或者去向相同,是可以缩成一个点的(想想就明白了),这样边的数目减少了运行的速度也会相应的提升, 这道题压缩的方式就是利用了m <= 10和二进制的原理,首先, 如果两个人的状态一样, 比如有10个星球, A喜欢星球1, 星球2, 星球3, 而B也喜欢星球1, 星球2, 星球3, 那么A和B就可以合并成一个点 C, 原来各自到喜欢的星球的流量为1, 现在合并之后到那几个星球的流量就成了人的数目 2。因为只有去和不去两种状态, 所以可以用0 和1 表示,这正好在二进制中可以表示出来,具体实现看代码
Accepted code
#include<bits/stdc++.h>
using namespace std;
#define sc scanf
#define mem memset
#define Min(a, b) a = min(a, b)
#define Max(a, b) a = max(a, b)
typedef long long ll;
const int MAXN = 2e5 + 10;
const int MAXM = 1e6 + 10;
const int INF = 0x3f3f3f3f;
struct Edge
{
int u, v, w, next;
}e[MAXM << 1];
vector <int> edge[MAXN];
int head[MAXN], n, m, t, cnt, T, X;
int dep[MAXN], cur[MAXN], sp, tp;
bool vis[MAXN];
void init() {
mem(e, 0, sizeof(e));
mem(head, -1, sizeof(head));
for (int i = 0; i <= 1024; i++) edge[i].clear();
cnt = 0;
}
void add(int from, int to, int w) {
e[cnt].v = to; e[cnt].w = w;
e[cnt].next = head[from]; head[from] = cnt++;
e[cnt].v = from; e[cnt].w = 0;
e[cnt].next = head[to]; head[to] = cnt++;
}
bool bfs() {
queue <int> q;
memset(dep, 0, sizeof(dep));
dep[sp] = 1, q.push(sp);
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = head[now]; i != -1; i = e[i].next) {
int vi = e[i].v;
if (!dep[vi] && e[i].w) {
dep[vi] = dep[now] + 1;
q.push(vi);
}
}
}
if (dep[tp]) return true;
else return false;
}
int dfs(int x, int dist) {
if (x == tp || dist == 0) return dist;
int res = 0, r;
for (int &i = cur[x]; i != -1; i = e[i].next) {
int vi = e[i].v;
if (dep[vi] == dep[x] + 1 && e[i].w) {
r = dfs(vi, min(e[i].w, dist - res));
if (r > 0) {
e[i].w -= r;
e[i ^ 1].w += r;
res += r;
if (res == dist) return dist;
}
}
}
if (!res) dep[x] = 0;
return res;
}
int dinic() {
int flow = 0, ans;
while (bfs()) {
for (int i = 0; i <= tp; i++) cur[i] = head[i];
flow += dfs(sp, INF);
}
return flow;
}
int main()
{
while (~sc("%d %d", &n, &m)) {
init();
sp = 0, tp = n + m + 1;
for (int i = 1; i <= n; i++) {
int ui = 0, vi;
for (int j = 0; j < m; j++) {
sc("%d", &vi);
if (vi) ui |= (1 << j);
}
edge[ui].push_back(i); // ui表示状态,存的是各个人
}
for (int i = 0; i < 1024; i++) { // ui表示压缩后的人的下标,表示人数目
if (edge[i].size() == 0) continue;
int ui = edge[i][0], sz = edge[i].size();
add(sp, ui, sz);
for (int j = 0; j < m; j++) { // 连接喜欢的边
if (i & (1 << j)) add(ui, j + 1 + n, sz);
}
}
for (int i = n + 1; i <= n + m; i++) {
int ui;
sc("%d", &ui);
add(i, tp, ui);
}
int ans = dinic();
if (ans == n) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}