POJ2908 Quantum Light Search + priority queue Good topic

Source: Internet
Author: User

This is a good topic ah, holiday back ready to practice practiced hand, found that I was weak explosion ... First of all, the general determination of the idea, drew a while, found that the priority queue directly greedy can, then knocked, started with a string led has been WA, did an afternoon, and then found the wrong place, then the tle, and then see Online said is not using the STL priority queue, I write a small heap, and then set up a template, the result or tle, think their template is wrong, but found that the same with others, and got the present, really can not find out where there are problems, and then see the problem, found a conversion to do, the string into binary decimal number, so that the direct use of bit operations to do , finally is a dropped, to try the memory of the search, because after the transformation has a status mark, tried several times, found no, gave up, a day to do this topic ...


Reference: Poke here

The practice is still relatively clear, draw a picture again bold guess, leave a memorial, after all, do a day, test instructions said not clear quote others: The topic gives NOP operation, by N (do Nothing), S (1), C (0), F (reverse) and other basic operations, each operation will produce different heat. The initial string and the target string of the NW group are given, which require the heat to be generated at least to convert the initial string into the target string, and there may be no solution at this time to output "NP"


Direct BFS, similar to DFS's brute force enumeration, the queue loops, each time to sweep all the NOP operations, and then spend a small priority, violence, find out, not find is not the answer, but also to mark whether this state has appeared, each answer must be the status tag array empty, A few days ago because the use of memset always tle, pit for a long time, this topic is still a bit empty, fortunately not be pits


After doing the topic to remind yourself to find the idea, to first think about how to do the most convenient, or else you get around the drunk ~


typedef struct NODE {int val;int ss;friend bool operator< (node X,node y) {return x.val > Y.val;}}; int N,nop,nw;int make_true[30 + 5],make_false[30 + 5],xo[30 + 5],w[30 + 5];bool vis[1<<22];p riority_queue<node &G T Q;void init () {memset (make_true,0,sizeof (make_true)); Memset (Make_false,0,sizeof (Make_false)); Memset (Xo,0,sizeof ( XO)); Memset (W,0,sizeof (w));} bool Input () {while (CIN&GT;&GT;N&GT;&GT;NOP&GT;&GT;NW) {for (int i=0;i<nop;i++) {char s[20 + 5];scanf ("%s%d", &s, &w[i]); for (int j=0;j<n;j++) {make_true[i] <<= 1;make_false[i] <<= 1;xo[i] <<= 1;make_false[i] ++;if (s[j] = = ' s ') Make_true[i]++;else if (s[j] = = ' F ') xo[i]++;else if (s[j] = = ' C ') make_false[i]--;}} return false;} return true;} int Slove (char *s) {int len = strlen (s); int ret = 0;for (int i=0;i<len;i++) {ret <<= 1;if (s[i] = = ' 1 ') ret++;} return ret;} int BFS (int start,int end) {memset (vis,false,sizeof (VIS)), while (!q.empty ()) Q.pop (); Node S,e;s.val = 0;S.SS = Start;q.push (s); while (!q.emptY ()) {s = Q.top (); Q.pop (), int pos = s.ss;if (Vis[pos]) continue;if (pos = = end) return S.val;vis[pos] = true;for (int i=0;i< nop;i++) {int tmp = ((Pos&make_false[i]) |make_true[i]) ^xo[i];if (!vis[tmp]) {E.SS = Tmp;e.val = S.val + W[i];q.push (e) ;}}} return-1;} void Cal () {int q = Nw;bool flag = False;while (q--) {char start[20 + 5],end[20 + 5];scanf ("%s%s", start,end); if (flag) Putch AR ('), else flag = true;int s = slove (start), int e = Slove (end), int ans = BFS (s,e), if (ans < 0) printf ("NP"), or else printf ("%d", ans);//cout<< "--";} Puts ("");} void output () {}int main () {int T;cin>>t;while (t--) {init (); if (input ()) return 0;cal (); output ();} return 0;}


POJ2908 Quantum Light Search + priority queue Good topic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.