题解:P13712 淘汰(Easy ver.)

· · 题解

前言

我能做出来的都是水题

思路

一道简单的广搜练手题,剩下的我都写在代码中了。

代码

全球唯一的丑陋码风

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node//定义结构体
{
    int num,cost; //数值和花费
};
int x,y,a,b,c,d,ans;
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        map<int,bool> v;//标记数组,因为数据太大了所以用的map
        ans=1e10;
        cin>>x>>y>>a>>b>>c>>d;
        queue<node> q;
        node begin;
        begin.num=x;
        begin.cost=0;
        v[x]=1;
        q.push(begin);
        while(q.size())
        {
            int num=q.front().num,cost=q.front().cost;
            q.pop();
            if(num==y)//当前数值等于答案y
            {
                ans=min(ans,cost);//更新花费的最小值
                continue;
            }

            v[num]=1;//标记,代表已经搜索过x经过操作之后等于num的情况了
            int nxt;
            nxt=num&a;
            if(!v[nxt])//没被搜索过
            {
                q.push({nxt,cost+c});//操作1(花费c)
            }
            nxt=num|b;
            if(!v[nxt])//没被搜索过
            {
                q.push({nxt,cost+d});//操作2(花费d)
            }
        }
        if(ans==1e10)cout<<-1<<endl;//如果ans没被更新就说明无解
        else cout<<ans<<endl;
    }
 }