PAT乙级1090 危险品装箱 (25 分)

本文介绍了一种用于检查货物间是否兼容的算法实现。通过使用C++编程语言,结合vector和map数据结构,该算法能够有效地判断一组货物清单中是否存在不兼容的货物组合。对于每个货物,算法记录了其不兼容的货物列表,并在遍历货物清单时检查是否存在冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

思路

把每个货物与之不容的保存在一个vector中,然后对输入的货物清单中的货物进行遍历,看是否有在vector中的

实现

#include <iostream>
#include <algorithm>
#include <vector>
#include<map>
using namespace std;


int main()
{
	int N, M, i,j;
	double temp1, temp2;
	cin >> N >> M;
	map<double, vector<double>> a, b;
	for (i = 0; i < N; i++)
	{
		cin >> temp1 >> temp2;
		a[temp1].push_back(temp2);
		a[temp2].push_back(temp1);
	}
	for (i = 0; i < M; i++)
	{
		int K,flag=1,gnum;
		cin >> K;
		vector<double> G;
		for (j = 0; j < K; j++)
		{
			cin >> gnum;
			if (a[gnum].size()==0)
				continue;
			G.push_back(gnum);
		}
			
		for (j = 0; j < G.size(); j++)
		{
			vector<double> ng = a[G[j]];
			for (int m = j; m < G.size(); m++)
			{
				if (find(ng.begin(), ng.end(), G[m]) != ng.end())
				{
					flag = 0;
					break;
				}
			}
			if (!flag)
				break;
		}
		if (!flag)
			cout << "No" << endl;
		if (flag)
			cout << "Yes" << endl;
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值