Kaidul

10681

Oct 28th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <cstring>
  4. #include <iostream>
  5. using namespace std;
  6. #define mem(x,y) memset(x,y,sizeof(x))
  7. #define READ(f) freopen(f, "r", stdin)
  8.  
  9. /**  Main Code **/
  10.  
  11. #define Max 500
  12. #define No_city 100
  13. #define No_days 200
  14.  
  15. bool visited[No_city + 1][No_days + 1];
  16. bool dp[No_city + 1][No_days + 1];
  17. vector<int> adj[Max + 10];
  18. int cities, edge, start, end, days;
  19.  
  20. bool tsp (int city, int days) {
  21.     if(days == 0) {
  22.         if (city == end)
  23.             return true;
  24.         else
  25.             return false;
  26.     }
  27.  
  28.     if (city == end)
  29.         return false;
  30.  
  31.     if (visited[city][days])
  32.         return visited[city][days];
  33.  
  34.     visited[city][days] = true;
  35.     bool ans = false;
  36.  
  37.     for (int i = 0; i < (int) adj[city].size(); i++) {
  38.         //ans = ans || tsp(adj[city][i], days - 1);
  39.     ans = tsp(adj[city][i], days - 1);
  40.         if(ans == true)
  41.             break;
  42.     }
  43.     return dp[city][days] = ans;
  44. }
  45.  
  46. int main() {
  47. #ifndef ONLINE_JUDGE
  48.     READ("input.txt");
  49. #endif
  50.     int u, v;
  51.     while(scanf("%d%d", &cities, &edge) != EOF and cities and edge) {
  52.         for (int i = 0; i < edge; i++) {
  53.             scanf("%d %d", &u, &v);
  54.             adj[u].push_back(v);
  55.             adj[v].push_back(u);
  56.         }
  57.         scanf("%d%d%d", &start, &end, &days);
  58.         mem(visited, false);
  59.         if(tsp(start, days)) {
  60.             printf("Yes, Teobaldo can travel.\n");
  61.         } else {
  62.             printf("No, Teobaldo can not travel.\n");
  63.         }
  64.         for (int i = 0; i < cities + 5; i++) {
  65.             adj[i].clear();
  66.         }
  67.     }
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment