zoukankan      html  css  js  c++  java
  • HDU-3605-Escape(最大流, 状态压缩)

    链接:

    https://vjudge.net/problem/HDU-3605

    题意:

    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.

    思路:

    1e5个人,每个人都连边会T, 考虑每个人的状态,选10个星球,最多1024中选择,吧人转换程选择,压缩成1024中情况.
    连到星球,在跑最大流即可.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    
    const int MAXN = 1e4+10;
    const int INF = 1e9;
    
    struct Edge
    {
        int from, to, cap;
    };
    vector<Edge> edges;
    vector<int> G[MAXN];
    int Dis[MAXN], Vis[MAXN];
    int Peo[MAXN];
    int n, m, s, t, d;
    
    void AddEdge(int from, int to, int cap)
    {
        edges.push_back(Edge{from, to, cap});
        edges.push_back(Edge{to, from, 0});
        G[from].push_back(edges.size()-2);
        G[to].push_back(edges.size()-1);
    }
    
    bool Bfs()
    {
        //Bfs构造分层网络
        memset(Dis, -1, sizeof(Dis));
        queue<int> que;
        que.push(s);
        Dis[s] = 0;
        while (!que.empty())
        {
            int u = que.front();
            que.pop();
    //        cout << u << endl;
            for (int i = 0;i < G[u].size();i++)
            {
                Edge & e = edges[G[u][i]];
                if (e.cap > 0 && Dis[e.to] == -1)
                {
                    que.push(e.to);
                    Dis[e.to] = Dis[u]+1;
                }
            }
        }
        return (Dis[t] != -1);
    }
    
    int Dfs(int u, int flow)
    {
        //flow 表示当前流量上限
        if (u == t)
            return flow;
        int res = 0;
        for (int i = 0;i < G[u].size();i++)
        {
            Edge & e = edges[G[u][i]];
            if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
            {
                int tmp = Dfs(e.to, min(flow, e.cap)); //  递归计算顶点 v
                flow -= tmp;
                e.cap -= tmp;
                res += tmp;
                edges[G[u][i]^1].cap += tmp;
                if (flow == 0)
                    break;
            }
        }
        if (res == 0)
            Dis[u] = -1;
        return res;
    }
    
    int MaxFlow()
    {
        int res = 0;
        while (Bfs())
        {
            res += Dfs(s, INF);
        }
        return res;
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        while (cin >> n >> m)
        {
            memset(Peo, 0, sizeof(Peo));
            s = 1023+m+1, t = 1023+m+2;
            for (int i = 0;i <= t;i++)
                G[i].clear();
            edges.clear();
            int w;
            for (int i = 1;i <= n;i++)
            {
                int res = 0;
                for (int j = 0;j < m;j++)
                {
                    cin >> w;
                    res += (1<<j)*w;
                }
    //            cout << res << endl;
                Peo[res]++;
            }
            for (int i = 1;i <= m;i++)
            {
                cin >> w;
                AddEdge(1023+i, t, w);
            }
            for (int i = 0;i <= 1023;i++)
            {
                if (Peo[i] == 0)
                    continue;
                AddEdge(s, i, Peo[i]);
                int tmp = i;
                int cnt = 1;
                while (tmp)
                {
                    if (tmp&1)
                    {
                        AddEdge(i, 1023+cnt, Peo[i]);
                    }
                    tmp >>= 1;
                    cnt++;
                }
            }
            int res = MaxFlow();
            if (res == n)
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
        }
    
        return 0;
    }
    /*
     2 2
     1 1
     0 1
     2 2
     */
    
  • 相关阅读:
    几个不错的网页载入页面
    .NET 中关于日期时间的格式化处理
    防止网站内容被人小偷和采集的ASP代码
    .Net学习资源集
    Net程序如何防止被注入(整站通用)
    一个采集入库生成本地文件的几个FUCTION
    网页数据采集小偷
    浅谈自动采集程序及入库
    网页图片处理JS代码整理
    spark导入工程后,出现一些错误
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11333720.html
Copyright © 2011-2022 走看看