Codeforces 1846G Rudolf and CodeVid-23

题目描述:

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A new virus called "CodeVid-23" has spread among programmers. Rudolf, being a programmer, was not able to avoid it.

There are nn symptoms numbered from 1 to n that can appear when infected. Initially, Rudolf has some of them. He went to the pharmacy and bought m medicines.

For each medicine, the number of days it needs to be taken is known, and the set of symptoms it removes. Unfortunately, medicines often have side effects. Therefore, for each medicine, the set of symptoms that appear when taking it is also known.

After reading the instructions, Rudolf realized that taking more than one medicine at a time is very unhealthy.

Rudolph wants to be healed as soon as possible. Therefore, he asks you to calculate the minimum number of days to remove all symptoms, or to say that it is impossible.

Input

The first line contains a single integer t (1≤t≤100)(1≤t≤100) — the number of test cases.

Then follow the descriptions of the test cases.

The first line of each test case contains two integers n,m(1≤n≤10,1≤m≤10^3)— the number of symptoms and medicines, respectively.

The second line of each test case contains a string of length n consisting of the characters 0 and 1 — the description of Rudolf's symptoms. If the i-th character of the string is 1, Rudolf has the i-th symptom, otherwise he does not.

Then follow 3⋅m lines — the description of the medicines.

The first line of each medicine description contains an integer d (1≤d≤10^3)— the number of days the medicine needs to be taken.

The next two lines of the medicine description contain two strings of length n, consisting of the characters 0 and 1 — the description of the symptoms it removes and the description of the side effects.

In the first of the two lines, 1 at position ii means that the medicine removes the i-th symptom, and 0 otherwise.

In the second of the two lines, 1 at position i means that the i-th symptom appears after taking the medicine, and 0 otherwise.

Different medicines can have the same sets of symptoms and side effects. If a medicine relieves a certain symptom, it will not be among the side effects.

The sum of m over all test cases does not exceed 10^3.

Output

For each test case, output a single integer on a separate line — the minimum number of days it will take Rudolf to remove all symptoms. If this never happens, output −1.

input

4
5 4
10011
3
10000
00110
3
00101
00000
3
01010
00100
5
11010
00100
4 1
0000
10
1011
0100
2 2
11
2
10
01
3
01
10
2 3
11
3
01
10
3
10
00
4
10
01

ouput

8
0
-1
6

题目大意:n中疾病,患病为1,不患病为0;m种药物,能治疗某种疾病为1,不能为0;问治疗患者所有疾病所需的最短天数是多少。

思路分析:n种疾病,有2^n次方种状态,我们将其状态压缩为十进制,即0~2^n-1,每种药物都能使一种状态转换为另一种状态,我们对这两个状态建边。最后对得到的图使用堆优化的迪杰斯特拉算法。

代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
const int MAXN=1e9;
using namespace std;
struct edge{
	int to,w,next;
}e[1024*1024];
int head[1050],cnt=0,dis[1050],visit[1050];
void add(int from,int to,int w){
	e[cnt].to=to;
	e[cnt].w=w;
	e[cnt].next=head[from];
	head[from]=cnt++;
}
struct node{
	int id,dis;
	bool operator<(const node &t)const{
		return dis>t.dis;
	}
};
int main(){
	int t;
	cin>>t;
	while(t--){
		cnt=0;
		memset(head,-1,sizeof(head));
		int n,m;
		cin>>n>>m;
		string a;
		cin>>a;
		int y=0,nn=1;
		for(int j=a.size()-1;j>=0;j--){
			y+=(a[j]-'0')*nn;
			nn*=2;
		}
		for(int i=1;i<=m;i++){
			int d;
			cin>>d;
			string a,b;
			cin>>a>>b;
			int x=0,y=0,k=1;
			for(int j=a.size()-1;j>=0;j--){
				y+=(a[j]-'0')*k;
				k*=2;
			}
			k=1;
			for(int j=b.size()-1;j>=0;j--){
				x+=(b[j]-'0')*k;
				k*=2;
			}
			for(int j=0;j<k;j++){
				int to=(j&~y)|x;
				add(j,to,d);
			}
		}
		for(int i=0;i<nn;i++)
		{
			dis[i]=MAXN;
			visit[i]=0;
		}
		dis[y]=0;
		node start;
		start.id=y;
		start.dis=0;
		priority_queue<node>res;
		res.push(start);
		while(!res.empty()){
			node t=res.top();
			res.pop();
			if(visit[t.id]==1)
			continue;
			visit[t.id]=1;
			for(int i=head[t.id];~i;i=e[i].next){
				int to=e[i].to,w=e[i].w;
				if(dis[to]>dis[t.id]+w){
					dis[to]=dis[t.id]+w;
				}
				node temp;
				temp.id=to;
				temp.dis=dis[to];
				res.push(temp);
			}
		}
		if(dis[0]!=MAXN){
			cout<<dis[0]<<endl;
		}
		else
		cout<<-1<<endl;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值