同 u v a 658 \frak{uva658} uva658
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<cctype>
using namespace std;
#define PII pair<int,int>
string Pre[105],Tar[105];
int S_positive[105]={};
int S_denegati[105]={};
int T_positive[105]={};
int T_denegati[105]={};
bool vis[2097157]={};
int N,M,T,dis[2097157]={},ans=0,val[105]={};
priority_queue<PII,vector<PII>,greater<PII> >Q;
int dijkstra(int S,int T)
{
for(int i=T;i<S;++i)dis[i]=0x3f3f3f3f;
memset(vis,0,sizeof(vis));
Q.push(make_pair(0,S)); dis[S]=0;
while(!Q.empty())
{
int x=Q.top().second; vis[x]=1; Q.pop();
for(int v,i=1;i<=M;++i)
{
if(!(((x&S_positive[i])==S_positive[i])&&((x&S_denegati[i])==x)))continue;
v=(x|T_positive[i])&T_denegati[i];
if(dis[v]<dis[x]+val[i])continue;
dis[v]=dis[x]+val[i];
if(vis[v])continue;
Q.push(make_pair(dis[v],v));
}
}
return dis[T];
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>N>>M;
for(int i=1;i<=M;++i)
{
cin>>val[i]>>Pre[i]>>Tar[i];
for(int j=0;j<N;++j)
{
if(Pre[i][j]=='+')S_positive[i]+=1<<j;
if(Pre[i][j]!='-')S_denegati[i]+=1<<j;
if(Tar[i][j]=='+')T_positive[i]+=1<<j;
if(Tar[i][j]!='-')T_denegati[i]+=1<<j;
}
}
ans=dijkstra((1<<N)-1,0);
if(!vis[T])cout<<'0';
else cout<<ans;
cout<<"\n\n";
return 0;
}