zoukankan      html  css  js  c++  java
  • 【洛谷 P2761】 软件补丁问题(状态压缩,最短路)

    题目链接
    第四题。

    初看题目很懵,网络流这么厉害的吗,毫无头绪去看题解。。
    所以这和网络流有什么关系呢?

    把规则用二进制保存下来,然后跑最短路救星了。
    在线跑,离线连边太慢了。
    (以后干脆不管什么题直接开100W,Re变成Wa调了我好久

    #include <cstdio>
    #include <queue>
    #include <cstring>
    #define INF 2147483647
    using namespace std;
    const int MAXN = 110;
    int b1[MAXN], b2[MAXN], f1[MAXN], f2[MAXN], t[MAXN];
    int n, m, now;
    int dis[1 << 21];
    char ch;
    queue <int> q;
    int main(){
        memset(dis, 127, sizeof dis);
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= m; ++i){
           scanf("%d", &t[i]);
           for(int j = 1; j <= n; ++j){
              ch = getchar();
              while(ch != '0' && ch != '+' && ch != '-') ch = getchar();
              if(ch == '+') b1[i] |= (1 << j - 1);
              if(ch == '-') b2[i] |= (1 << j - 1);
           }
           for(int j = 1; j <= n; ++j){
              ch = getchar();
              while(ch != '0' && ch != '+' && ch != '-') ch = getchar();
              if(ch == '-') f1[i] |= (1 << j - 1);
              if(ch == '+') f2[i] |= (1 << j - 1);
           }
        }
        q.push((1 << n) - 1);
        dis[q.front()] = 0;
        while(q.size()){
          now = q.front(); q.pop();
          for(int i = 1; i <= m; ++i)
             if(((now & b1[i]) == b1[i]) && (!(now & b2[i]))){
               int to = ((now | f1[i]) ^ f1[i]) | f2[i];
               if(dis[to] > dis[now] + t[i]){
                 dis[to] = dis[now] + t[i];
                 q.push(to);
               }
             }
        }
        printf("%d
    ", dis[0] > 100000000 ? 0 : dis[0]);
        return 0;
    }
    
    
  • 相关阅读:
    Python基础-编码转化
    Python基础-简要说明
    Python基础-流程控制
    Python基础-运算符
    Python基础-输入输出
    博客更新第一天 愿在前端路上 坚定不移 多累积
    bower解决js库的依赖管理
    NPM下载出错 No compatible version found
    jquery-1.10.2 获取checkbox的checked属性总是undefined
    HTML中的下拉列表 select
  • 原文地址:https://www.cnblogs.com/Qihoo360/p/10152094.html
Copyright © 2011-2022 走看看