CSU-ACM2017暑假集训比赛8 - E - Escape - HDU - 3605

E - Escape

 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

进行状态压缩后跑最大流即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 3004, INF = 0x3f3f3f3f;
struct Edge {
    int from, to, cap, flow;
    Edge(){}
    Edge(int ff, int t, int c, int f):from(ff), to(t), cap(c), flow(f){}
};
int N, M;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn], cur[maxn];
struct Dinic {
    int n, m, s, t;
    void init() {
        for(int i = 0; i <= maxn; i++)
            G[i].clear();
        edges.clear();
        memset(vis, false, sizeof(vis));
        memset(d, 0, sizeof(d));
        memset(cur, 0, sizeof(cur));
    }
    void makeEdge(int from, int to, int cap) {
        edges.push_back(Edge(from, to, cap, 0));
        edges.push_back(Edge(to, from, 0, 0));
        m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
    }

    bool BFS() {
        memset(vis, 0, sizeof(vis));
        queue<int> Q;
        Q.push(s);
        d[s] = 0;
        vis[s] = true;
        while(!Q.empty()) {
            int x = Q.front();
            Q.pop();
            for(int i = 0; i < G[x].size(); i++) {
                Edge &e = edges[G[x][i]];
                if(!vis[e.to] && e.cap > e.flow) {
                    vis[e.to] = true;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x, int a) {
        if(x == t || a == 0)
            return a;
        int flow = 0, f;
        for(int &i = cur[x]; i < G[x].size(); i++) {
            Edge &e = edges[G[x][i]];
            if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
                e.flow += f;
                edges[G[x][i] ^ 1].flow -= f;
                flow += f;
                a -= f;
                if(a == 0)
                    break;
            }
        }
        return flow;
    }

    int maxFlow(int s, int t) {
        this->s = s;
        this->t = t;
        int flow = 0;
        while(BFS()) {
            memset(cur, 0, sizeof(cur));
            flow += DFS(s, INF);
        }
        return flow;
    }
};

int main() {
#ifdef TEST
    freopen("test.txt", "r", stdin);
#endif // TEST

    char res[][4] {"NO", "YES"};
    while(cin >> N >> M) {
        Dinic Gf;
        Gf.init();
        int source = 0, terminal = (1<<M)+M+1;
        int status[1080], temp;
        memset(status, 0, sizeof(status));
        for(int i = 0; i < N; i++){
            int S = 0;
            for(int j = 0; j < M; j++){
                scanf("%d", &temp);
                S |= (temp<<(M-1-j));
            }
            status[S]++;
        }
        for(int i = 1; i <= (1<<M); i++){
            if(status[i]){
                Gf.makeEdge(source, i, status[i]);
                int x = i, dig = M;
                while(x){
                    if(x & 1)
                        Gf.makeEdge(i, (1<<M)+dig, INF);
                    x >>= 1;
                    dig--;
                }
//                for(int j = 0; j < M; j++)
//                    if(i & (1<<j))
//                        Gf.makeEdge(i, (1<<M)+j+1, INF);
            }
        }
        for(int i = 1; i <= M; i++){
            int capability;
            scanf("%d", &capability);
            Gf.makeEdge((1<<M)+i, terminal, capability);
        }
        int mf = Gf.maxFlow(source, terminal);
        printf("%s\n", res[mf == N]);
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值