【洛谷 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\n", dis[0] > 100000000 ? 0 : dis[0]);
    return 0;
}

转载于:https://www.cnblogs.com/Qihoo360/p/10152094.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值