2908

/*
优先队列,注意直接用string速度比较慢,总之用STL比较慢
自己写堆,数据结构可能会比较快
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long ll;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)

const double Pi = acos(-1.0);

#define TMIN(x,y) ( x<y?x:y )

#define DEBUG

// code begin
#define MAXN (1<<20)+1
int used[MAXN];
int T;
int L,Nop,Nw;
char op[40][22];
int opcost[40];
char from[22];
char to[22];

int hash(string s)
{
	int ans = 0;
	for(int i=0;i<L;i++)
	{
		ans = ans*2+s[i]-'0';
	}
	return ans;
}

int modify(int s,int k)
{
	int ans = 0;

	for(int i=0;i<L;i++)
	{
		bool t=s&(1<<(L-i-1));
		if( op[k][i]=='N')
		{
			;
		}
		else if(op[k][i]=='F')
		{
			t = 1-t;
		}
		else if(op[k][i]=='S')
		{
			t = 1;
		}
		else if(op[k][i]=='C')
		{
			t = 0;
		}
		ans = ans*2+t;

	}
	return ans;
}

struct Node
{
	Node(){}
	Node(int a,int b)
	{
		str=  a;
		time = b;
	}
	int str;
	int time;
};

struct cmp
{
	bool operator()(Node &a,Node &b)
	{
		return a.time>b.time;
	}
};

int main()
{
#ifdef DEBUG
	read;
	write;
#endif
	char in[22];
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d %d %d",&L,&Nop,&Nw);
		for(int i=0;i<Nop;i++)
		{
			scanf("%s %d",op[i],&opcost[i]);
		}

		for(int i=0;i<Nw;i++)
		{
			scanf("%s %s",from,to);
			//for(int j=0;j<MAXN;j++) used[j] = MAXN;
			memset(used,-1,sizeof(used));
			int first=hash(from);
			int end=hash(to);
			priority_queue<Node,vector< Node >, cmp> qps;
			qps.push( Node(first,0));
			used[ first] = 0;
			int ans = MAXN;
			while(!qps.empty())
			{
				Node cur = qps.top();qps.pop();
				
				if( cur.str==end )
				{
					ans = cur.time;
					break;
				}

				for(int k=0;k<Nop;k++)
				{
					int t2 = modify(cur.str,k);
					int vl = cur.time+opcost[k];
					if( used[t2]==-1 || vl<used[t2] )
					{
						used[t2] = vl;
						qps.push(Node(t2,vl));
					}
				}
			}

			if( ans==MAXN) printf("NP");
			else printf("%d",ans);
			if( i!=Nw-1) printf(" ");
			else printf("\n");
		}
	}
	return 0;
}

posted @ 2011-05-28 14:35  AC2012  阅读(162)  评论(0编辑  收藏  举报