91分求调
查看原帖
91分求调
577363
cenzhiy楼主2025/7/28 17:00
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int n, m, tim[105];
vector<int> b1[105], b2[105], f1[105], f2[105];
priority_queue < pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
int fastmi(int x, int y) {
	if (y == 0) {
		return 1;
	}
	if (y == 1) {
		return x;
	}
	int bb = fastmi(x, y / 2);
	if (y % 2 == 0) {
		return bb * bb;
	} else {
		return bb * bb * x;
	}
}
bool visit[1050005];
int bfs(int yy) {
	while (!q.empty()) {
		q.pop();
	}
	pair<int, int> p;
	p.first = 0;
	p.second = yy;
	memset(visit, 0, sizeof(visit));
	visit[yy] = true;
	q.push(p);
	while (!q.empty()) {
		int x = q.top().second;
		int c = q.top().first;
		q.pop();
		int now[25];
		int dd = x;
		for (int i = 1; i <= n; i++) {
			now[i] = dd % 2;
			dd /= 2;
		}
		if (x == 0) {
			return c;
		}
		for (int i = 1; i <= m; i++) {
			bool flag = false;
			for (int j = 0; j < b2[i].size(); j++) {
				if (now[b2[i][j]] == 1) {
					flag = true;
					break;
				}
			}
			if (flag) {
				continue;
			}
			for (int j = 0; j < b1[i].size(); j++) {
				if (now[b1[i][j]] == 0) {
					flag = true;
					break;
				}
			}
			if (flag) {
				continue;
			}
			int now2[25];
			for (int j = 1; j <= n; j++) {
				now2[j] = now[j];
			}
			for (int j = 0; j < f1[i].size(); j++) {
				now2[f1[i][j]] = 0;
			}
			for (int j = 0; j < f2[i].size(); j++) {
				now2[f2[i][j]] = 1;
			}
			int zhi = 0;
			for (int j = n; j >= 1; j--) {
				zhi = zhi * 2 + now2[j];
			}
			if (!visit[zhi]) {
				visit[zhi] = true;
				pair<int, int> pp;
				pp.first = c + tim[i];
				pp.second = zhi;
				q.push(pp);
			}
		}
	}
	return 0;
}
int main() {
	cin >> n >> m;

	for (int i = 1; i <= m; i++) {
		string s;
		cin >> tim[i];
		cin >> s;
		for (int j = 0; j < n; j++) {
			if (s[j] == '+') {
				b1[i].push_back(j+1);
			} else if (s[j] == '-') {
				b2[i].push_back(j+1);
			}
		}
		cin >> s;
		for (int j = 0; j < n; j++) {
			if (s[j] == '-') {
				f1[i].push_back(j+1);
			} else if (s[j] == '+') {
				f2[i].push_back(j+1);
			}
		}
	}
	int result = bfs(fastmi(2, n) - 1);
	cout << result << endl;
	return 0;
}

不知道为什么第9个测试点就是过不去

2025/7/28 17:00
加载中...