zoukankan      html  css  js  c++  java
  • Tyvj3308毒药解药题解

    • 题目大意
      这些药都有可能在治愈某些病症的同一时候又使人患上某些别的病症……经过我天才的努力。最终弄清了每种药的详细性能,我会把每种药能治的病症和能使人患上的病症列一张清单给你们,然后你们要依据这张清单找出能治愈全部病症的最少药剂组合……顺便说一声,病症的数目不超过10种。我的药是用不完的,就是说每种药剂都能够被反复使用。

    • 题解
      二进制表示患病状态(2n1024种)和每种药的治病与致病状态,然后从2n1到0開始连有向边,然后bfs出最短路就可以。感觉这样对付一道搜索题大材小用了。

    • Code

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int maxn = 200000, nil = 0, maxm = 21, oo = 1000000000;
    int n, m, map[maxm][105];
    int cur[105], inf[105];//cure infect
    int u[maxn], v[maxn], nxt[maxn], pnt[1 << maxm], e;
    bool vis[1 << maxm];
    int d[1 << maxm];
    void add(int a, int b)
    {
        u[++e] = a; v[e] = b;
        nxt[e] = pnt[a]; pnt[a] = e;
    }
    void init()
    {
        int x;
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= m; ++i)
        {
            for(int j = 1; j <= n; ++j)
            {
                scanf("%d", &x);
                if(x == 1)
                {
                    cur[i] |= (1 << (j - 1));
                }
                if(x == -1)
                {
                    inf[i] |= (1 << (j - 1));
                }
            }
        }
        for(int i = (1 << n) - 1; i > 0; --i)
        {
            for(int j = 1; j <= m; ++j)
            {
                add(i, (i & (~cur[j])) | inf[j]);
            }
        }
    }
    void work()
    {
        queue <int> Q;
        memset(d, 0x3f, sizeof(d));
        Q.push((1 << n) - 1);
        d[(1 << n) - 1] = 0;
        vis[(1 << n) - 1] = true;
        while(!Q.empty())
        {
            int t = Q.front();
            Q.pop();
            for(int j = pnt[t]; j != nil; j = nxt[j])
            {
                if(!vis[v[j]])
                {
                    d[v[j]] = d[t] + 1;
                    vis[v[j]] = true;
                    Q.push(v[j]);
                }
            }
        }
        if(d[0] > oo) puts("The patient will be dead.");
        else printf("%d
    ", d[0]);
    }
    int main()
    {
        init();
        work();
        return 0;
    }
  • 相关阅读:
    Unity The Method Signature Matching Rule
    Unity The Property Matching Rule
    Unity The Type Matching Rule
    Unity The Custom Attribute Matching Rule
    Unity The Member Name Matching Rule
    Unity No Policies
    Unity The Return Type Matching Rule
    Unity The Parameter Type Matching Rule
    Unity The Namespace Matching Rule
    关于TSQL递归查询的(转)
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7222229.html
Copyright © 2011-2022 走看看