C-Language BFS (5) ___tt and magician (swust OJ 2464)

Source: Internet
Author: User

Description
    TT lives in a country full of magic, for ease of management, the king asked the magician to create a "rainbow bridge" between some important cities! The special thing about the Rainbow Bridge is that it can move from one end of the bridge to the other at a moment. The king also asked the magician to design a pass for the Rainbow Bridge, the pass is roughly divided into a,b,c three kinds, Rainbow Bridge also corresponds to a,b,c three kinds, each rainbow Bridge can be recognized by up to three passes, everyone has a unique type of pass, the person with the pass can be in the corresponding Rainbow bridge back and forth, If you have a pass, you can only shuttle to the Rainbow Bridge that identifies a pass.    one day, TT because there is urgent need to shuttle from City 1 to the city N, obviously, a type of pass may not make him smooth from City 1 to City N. So he asked the wizard to help him see if he could design a universal pass so that he could shuttle any Rainbow bridge. The wizard is exhausted, finally designed a can change the pass, this pass by the wizard with magic potion soaked for 7,749 days, condensed the wizard's all wisdom, finally has a magical transformation function, so that it will automatically after each use into another type of pass, the order of the transformation is a->b- >c->a.. so loop. Suppose the first type is a. Would you please tell TT if he can use this magic card smoothly from 1 to n? Of course, because there is a fee to pay for each transfer, select the least expensive one if there are multiple paths.
Input
   Number of first behavior test group T, each set of sample samples the first line input 2 number n,m for N cities There is M Rainbow Bridge (1<=n<=1000,0<=m<=n*n). Next m line, each line of four digits S T L C (s!=t,1<=l<=1000,1<=c<=3) represents the city S and T (1<=s,t<=n) need L toll and type C (for convenience, we use 1, 2,3 represents the type of pass A,b,c respectively) pass (all numbers are positive integers).
Output
If TT can go smoothly from City 1 to City N, output the final minimum cost, no output-1
Sample Input
24 31 2 1 12 3 2 23 4 3 3 4 31 2 1 12 3 2 23 4 3 2
Sample Output
6-1
original title Link: http://www.oj.swust.edu.cn/problem/show/2464


Analysis:The first thing is to use the memory search, and then the ruthless WA many times, and then only randomly generated data with the AC code to run, and finally ran thousands of sets of data to find the reason, the problem of memory search in this problem is if the path sequential reading problems may lead to another point after a point B, If there is a path from B to a and then to N, this time because a point first, a is marked, and then B can not reach N point through a, the data of B will be remembered as unreachable destination. However, the mark cannot be canceled, otherwise it will be looped in the loop map. This problem is only one of the problems of memory search, but the code has changed many times or WA.Here's the code for the memory search:
#include <cstdio> #include <cstring> #include <vector> #include <iostream> #include <queue > #include <stdlib.h>using namespace std;int memory[1001][1001][4],n,m;int mpt[1001][1001][4],money[1001][ 1001][4];bool Book[1001][1001][4]; vector<int>way[1001]; int Min (int a,int b) {return (a>b) b:a;} int dfs (int pre,int step,int k) {if (step==n) return 0;if (Memory[pre][step][k]) return memory[pre][step][k];book[pre][ Step][k]=true;int i,ans=999999999;for (I=0;i<way[step].size () i++) {//cout<<step<< "→" << way[ step][i]<<endl;//printf ("book[%d][%d]=%d\n", way[step][i],k%3+1,book[way[step][i]][k%3+1]); if (mpt[step][ way[step][i]][k]==0 | | book[step][way[step][i]][k%3+1]==true) continue;//cout<<step<< "→" << way[step][i]<< "K:" <<k<<endl;ans=min (Ans,dfs (step,way[step][i],k%3+1) +money[step][way[step][i]][k]);} book[pre][step][k]=false;//printf ("memort[%d][%d]=%d\n", Step,k,ans); if (ans!=999999999) memory[pre][step][K]=ans;return ans;} void init () {int i;for (i=0;i<1001;i++) way[i].clear (); memset (memory,0,sizeof (memory)); Memset (Money,0,sizeof ( Money)); Memset (Mpt,0,sizeof (MPT)); memset (book,0,sizeof (book));} int main () {int I,j,v,u,k,w,t,ans;cin>>t;while (t--) {cin>>n>>m;init (); for (i=0;i<m;i++) {scanf ( "%d%d%d%d", &v,&u,&w,&k); if (mpt[v][u][1]==0 && mpt[v][u][2]==0 && mpt[v][u][3]==0) { Way[v].push_back (u); Way[u].push_back (v);} if (money[v][u][k]==0) money[v][u][k]=w,money[u][v][k]=w;else{money[v][u][k]=min (money[v][u][k],w); money[u][v][k ]=min (money[u][v][k],w);} Mpt[u][v][k]=mpt[v][u][k]=1; }ans=dfs (0,1,1); if (ans!=999999999) printf ("%d\n", ans), else printf (" -1\n");}  return 0;  }



then use SPFA to do the problem:
#include <cstdio> #include <cstring> #include <vector> #include <iostream> #include <queue > #include <stdlib.h>using namespace std;struct node{int data,v,w;}; struct node1{int step,v;};  int N,m;int visit[1001][4],dis[1001][4]; The visit array is the record of the current state [node number] [pass number] is in the queue. Vector<node>mpt[1001];void SPFA () {Node1 S,e;int i,j,ss;queue<node1 >team;s.step=1;s.v=1;team.push (s); Visit[1][1]=1;while (Team.size ()) {//Use the status in the queue to update the minimum cost. S=team.front (); Team.pop (); Visit[s.step][s.v]=0;ss=mpt[s.step].size (); for (i=0;i<ss;i++) {if (MPT[S.STEP][I].V!=S.V) continue;if (dis[ S.STEP][S.V]+MPT[S.STEP][I].W&GT;=DIS[MPT[S.STEP][I].DATA][S.V%3+1]) continue;  DIS[MPT[S.STEP][I].DATA][S.V%3+1]=DIS[S.STEP][S.V]+MPT[S.STEP][I].W; Relaxation if (visit[mpt[s.step][i].data][s.v%3+1]!=0) continue;e.step=mpt[s.step][i].data;e.v=s.v%3+1;visit[e.step][ E.v]=1;team.push (e);}} int Min=1999999999;for (i=1;i<4;i++) if (Min>dis[n][i]) min=dis[n][i];if (min!=1999999999) cout<<min< <endl;else cout<< "-1" <<endl; } int main () {int I,j,t,x,y;cin>>t;while (t--) {Cin>>n>>m;memset (visit,0,sizeof (visit)); for (i=1;i <=n;i++) for (j=0;j<4;j++) dis[i][j]=1999999999;dis[1][1]=0;for (i=0;i<1001;i++) mpt[i].clear (); for (i=0;i<m;i++) {node s;cin>>x>>y>>s.w>>s.v;s.data=x;mpt[y].push_back (s); s.data=y; Mpt[x].push_back (s);} SPFA ();} return 0;}

If a great God can use a memory search to make trouble post the comments below, thank you very much!!!!


C-Language BFS (5) ___tt and magician (swust OJ 2464)

Related Article

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.