跳到正文

题解

题解:P13712 淘汰(Easy ver.)

qiaochu约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年8月13日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 2 分钟
题目
P13712

版本

  1. v1当前

前言

我能做出来的都是水题

思路

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

代码

全球唯一的丑陋码风

C++
#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;
	}
 } 
原文