(connected + violent) UVA-208 Firetruck

Source: Internet
Author: User

Original title Link:

More trouble, do not hang, you can on the UVA live find, you can also use virtual judge hanging problems.

Test instructions

Outputs all paths from 1 to K and cannot be repeated

Analysis:

The problem is simple deep search backtracking, using an array to record the path, the output when the condition is satisfied. Purple Book said need to first determine whether 1 to K is connected, otherwise it will time out. Made a direct deep search, sure enough tle. So we need to get connected first.

The method of connecting is (can be thought of):

1, DFS and BFS, both feel the same, but I choose the BFS

2, floyd,n^3 complexity, but more rude direct, also better write

3. To be added

Sentiment:

No pre-judgment connected 3000ms timeout, sentenced to connect on 0ms seconds ac, feeling ah.

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <Set>6#include <map>7#include <algorithm>8#include <string>9#include <queue>Ten#include <cmath> One#include <stack> A#include <cctype> -#include <list> -  the #definell Long Long - #defineull unsigned long Long - #defineVName (name) (#name) - #defineDebug (a) Cout<<vname (a) << "=" << (a) <<endl; +  - using namespacestd; +  A Const intMAXN =100010; at Const intINF =1<< -; - intK; -vector<int> edge[ A]; -queue<int>Q; - BOOLvis[ A]; - introad[ A]; in intsum; -  to voidinit () { +memset (Vis,0,sizeof(Vis)); -      for(intI=0; i< A; i++) { the edge[i].clear (); *     } $sum=0;Panax Notoginseng } -  the //a very standard wide-search + BOOLBFsintv) { A     BOOLmark[ A]= {0}; the      while(!q.empty ()) Q.pop (); + Q.push (v); -mark[v]=1; $      while(!Q.empty ()) { $         ints=Q.front (); - Q.pop (); -         if(s==k)return true;//find the K point and return the          for(intI=0; I<edge[s].size (); i++) { -             if(!Mark[edge[s][i]]) {Wuyi Q.push (Edge[s][i]); themark[edge[s][i]]=1; -             } Wu         } -     } About     return false; $ } -  -  - voidDfsintVintCNT) { ARoad[cnt]=v;//Record Path +     if(v==k) { the          for(intI=0; i<cnt; i++) { -printf"%d", Road[i]); $         } theprintf"%d\n", road[cnt]); thesum++;//number of record paths the         return ; the     } -      for(intu=0; U<edge[v].size (); u++) { in         if(!Vis[edge[v][u]]) { thevis[v]=1;//Mark Origin theDFS (edge[v][u],cnt+1); Aboutvis[v]=0;//Backtracking the         } the     } the } +  - intMain () { the     //Iostream::sync_with_stdio (false);Bayi  the #ifndef Online_judge theFreopen ("In.txt","R", stdin); -     //freopen ("OUT.txt", "w", stdout); - #endif the  the     intKace=0; the      while(~SCANF ("%d",&k)) { theInit ();//preprocessing empty -         intV,u; the          while(SCANF ("%d%d",&v,&u)) { the             if(v==0&&u==0) Break; the edge[v].push_back (u);94Edge[u].push_back (v);//Non-directional graph bidirectional connecting edge the         } the          for(intI=0; i< A; i++) { theSort (Edge[i].begin (), Edge[i].end ());//sorting, it is convenient to let Dfs out of the path by dictionary order98         } Aboutprintf"Case %d:\n",++kace); -         if(BFS (1)) DFS (1,0);//if connected, start deep search101printf"there is%d routes from the firestation to Streetcorner%d.\n", sum,k);102     }103     return 0;104}

(connected + violent) UVA-208 Firetruck

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.