【最短路+状压】CF1846 G

Problem - G - Codeforces

题意:

思路:

Code:

#include <bits/stdc++.h>
 
//#define int long long
 
using namespace std;
 
const int mxn=1e6+10;
const int mxv=1e6+10;
const int mxe=2e3+10;
const int mod=1e9+7;
const int Inf=0x3f3f3f3f;

struct ty2{
    int x,dis;
    bool operator<(const ty2&a)const{
        return a.dis<dis;
    }
};

priority_queue<ty2> Q;

int N,M,st=0;
int d[mxn],e[mxn],se[mxn];
int vis[(1<<10)+10],dis[(1<<10)+10];

char x;

void dij(){
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[st]=0;
    Q.push({st,0});
    while(!Q.empty()){
        auto u=Q.top();
        Q.pop();
        if(vis[u.x]) continue;
        vis[u.x]=1;
        for(int i=1;i<=M;i++){
            int w=d[i];
            int v=((u.x&(~e[i]))|se[i]);
            if(dis[v]>dis[u.x]+w){
                dis[v]=dis[u.x]+w;
                Q.push({v,dis[v]});
            }
        }
    }
}
void solve(){

    st=0ll;
    while(!Q.empty()) Q.pop();

    cin>>N>>M;

    for(int i=1;i<=M;i++){
        d[i]=e[i]=se[i]=0;
    }
    for(int i=0;i<N;i++){
        cin>>x;
        st|=((x=='1')<<i);
    }
    for(int i=1;i<=M;i++){
        cin>>d[i];
        e[i]=se[i]=0;
        for(int j=0;j<N;j++){
            cin>>x;
            e[i]|=((x=='1')<<j);
        }
        for(int j=0;j<N;j++){
            cin>>x;
            se[i]|=((x=='1')<<j);
        }
    }
    dij();
    if(dis[0]==Inf) cout<<-1<<'\n';
    else cout<<dis[0]<<'\n';
}
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int __=1;cin>>__;
    while(__--)solve();return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值