求卡空间
查看原帖
求卡空间
533160
rainbow_cat楼主2024/11/30 09:20

以下是本人的 code

#include<bits/stdc++.h>
using namespace std;
int n,m,t[110],b1[110],b2[110],f1[110],f2[110],dis[(1<<21)];
bool vis[(1<<21)];
string s;
vector<pair<int,int>>e[(1<<21)+10];
struct node{int u,dis;};
bool operator < (node x,node y){return x.dis>y.dis;};
priority_queue<node>q;
void dijkstra(int st)
{
	memset(dis,0x3f,sizeof dis);
	dis[st]=0,q.push({st,0});
	while(q.size())
	{
		int u=q.top().u;
		q.pop();
		if(vis[u])continue;
		vis[u]=1;
		if(u==0)return;
		for(auto i:e[u])
		{
			if(dis[i.first]>dis[u]+i.second)
			{
				dis[i.first]=dis[u]+i.second;
				q.push({i.first,dis[i.first]});
			}
		}
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>t[i]>>s;
		for(int j=0;j<s.size();j++)
		{
			if(s[j]=='+')b1[i]++;
			if(s[j]=='-')b2[i]++;
			b1[i]<<=1,b2[i]<<=1;
		}
		cin>>s;
		for(int j=0;j<s.size();j++)
		{
			if(s[j]=='-')f1[i]++;
			if(s[j]=='+')f2[i]++;
			f1[i]<<=1,f2[i]<<=1;
		}
		b1[i]>>=1,b2[i]>>=1,f1[i]>>=1,f2[i]>>=1;
	}
	for(int i=0;i<(1<<n);i++)
		for(int j=1;j<=m;j++)
			if((i&b1[j])==b1[j]&&!(i&b2[j]))
				e[i].push_back({(i&(~f1[j]))|f2[j],t[j]});
	dijkstra((1<<n)-1);
	if(dis[0]==0x3f3f3f3f)dis[0]=0;
	cout<<dis[0];
	return 0;
}
/*
1 0 1
1 1 0
0 0 0
0 1 0

1 0 1
1 1 1
0 1 1
0 0 0
*/

在洛谷 AC 然而某校内 OJ 却会 MLE 90pts

2024/11/30 09:20
加载中...