unlucky_13

uva_208

Jun 13th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. /*
  2.  Author                             :     unlucky_13
  3.  Problem_link                       :
  4.  Category                           :
  5.  Algorithm_Used                     :Bactracking _ The main optimaization comes from floyed marshell if there is no path of any lenght from u to n(~dis[u][n]=0) we won't follow the path beacuse this will give no solution
  6.  
  7.  */
  8.  
  9. #include<cstdio>
  10. #include<sstream>
  11. #include<cstdlib>
  12. #include<cctype>
  13. #include<cmath>
  14. #include<algorithm>
  15. #include<set>
  16. #include<queue>
  17. #include<stack>
  18. #include<list>
  19. #include<iostream>
  20. #include<fstream>
  21. #include<numeric>
  22. #include<string>
  23. #include<vector>
  24. #include<cstring>
  25. #include<map>
  26. #include<iterator>
  27. #define LL long long int
  28. //const long long int inf = 2147483647 ;
  29. //const int minx=;
  30. const int maxn=30;
  31.  
  32. using namespace std;
  33. int n ,adj[maxn][maxn],path[maxn],taken[maxn],res,dis[maxn][maxn];
  34.  
  35. void FW(){
  36.  
  37.  
  38.     for(int k=0;k<21;k++){
  39.         for(int i=0;i<21;i++){
  40.             for(int j=0;j<21;j++){
  41.                 dis[i][j] = dis[i][j]||(dis[i][k] && dis[k][j]) ;
  42.             }
  43.         }
  44.     }
  45.  
  46.     /*for(int i=0;i<21;i++){
  47.         for(int j=0;j<21;j++) printf("%d ",dis[i][j]) ;
  48.  
  49.     }*/
  50. }
  51.  
  52.  
  53.  
  54.  
  55. void BT(int u,int len){
  56.     if(u==n){
  57.         res++ ;
  58.         printf("1") ;
  59.         for(int i=0;i<len;i++) printf(" %d",path[i]) ;
  60.         printf("\n") ;
  61.     }
  62.     else if(dis[u][n]){
  63.         for(int i=0;i<21;i++){
  64.             if(adj[u][i] && taken[i]==0){
  65.                 taken[i] = 1 ;
  66.                 path[len]=i ;
  67.                 BT(i,len+1) ;
  68.                 taken[i]=0 ;
  69.             }
  70.         }
  71.     }
  72. }
  73.  
  74. int main() {
  75.  
  76.     int x,y,ct=1;
  77.     freopen("C:\\Users\\Mazhar\\Desktop\\in.txt", "r", stdin);
  78.     while(scanf("%d",&n)==1){
  79.         printf("CASE %d:\n",ct++) ;
  80.         res = 0 ;
  81.         memset(adj,0,sizeof(adj)) ;
  82.         memset(dis,0,sizeof(dis)) ;
  83.         while(1){
  84.             scanf("%d %d",&x,&y) ;
  85.             dis[x][y]=dis[y][x] = adj[x][y]=adj[y][x]=1 ;
  86.  
  87.             if(x==0 && y==0) break ;
  88.         }
  89.         memset(taken,0,sizeof(taken)) ;
  90.         FW() ;
  91.         taken[1]=1 ;
  92.         BT(1,0) ;
  93.         printf("There are %d routes from the firestation to streetcorner %d.\n",res,n) ;
  94.  
  95.  
  96.     }
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment