[题解] LP2761 软件补丁问题

Author Avatar
空気浮遊 2018年02月09日
  • 在其它设备中阅读本文章

本题跟网络流没啥关系...
虽然我想用网络流做,但是并找不到网络流的题解 \_(:зゝ∠)_

Problem

自己去原 OJ 看

Solution

$2^{21}-1$ 个状态当节点。最短路:能否转移状态,以及转移之后的状态。这里要用位运算。
乱搞一阵。
自己想也行的。反正不难。

Code

// Code by ajcxsu
// Problem: LP2761

#include<bits/stdc++.h>
using namespace std;

int U;
const int N=1<<21,M=110;
struct Package{
    int req, ned;
    int clr, rep;
    Package() { req=ned=0, clr=U, rep=0; }
    bool idt(int x) {
        x&=req;
        if((x^ned)==0) return true;
        else return false;
    }
    int operator +(int x) {
        x&=clr;
        x|=rep;
        return x;
    }
} p[M];
int w[M];
int n,m;
int dist[N];

int main() {
    cin>>n>>m;
    U=(1<<n)-1;
    for(int i=1;i<=m;i++) {
        Package &np=p[i];
        np=Package();
        cin>>w[i];
        char ch;
        for(int j=0;j<n;j++) {
            cin>>ch;
            if(ch!='0') np.req|=1<<j;
            if(ch=='+') np.ned|=1<<j;
        }
        for(int j=0;j<n;j++) {
            cin>>ch;
            if(ch!='0') np.clr^=1<<j;
            if(ch=='+') np.rep|=1<<j;
        }
    }
    memset(dist,0x3f,sizeof(dist));
    queue<int> qu;
    dist[U]=0;
    qu.push(U);
    int na;
    while(!qu.empty()) {
        na=qu.front(), qu.pop();
        for(int i=1;i<=m;i++)
            if(p[i].idt(na) && dist[p[i]+na]>dist[na]+w[i]) {
                dist[p[i]+na]=dist[na]+w[i];
                qu.push(p[i]+na);
            }
    }
    if(dist[0]==0x3f3f3f3f) cout<<0<<endl;
    else cout<<dist[0]<<endl;
    return 0;
}