pranavsindura

UVA 11635 WA

Nov 1st, 2020
1,286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long int
  3. #define ld long double
  4. #define pi pair<int,int>
  5. #define all(x) x.begin(), x.end()
  6. #define allr(x) x.rbegin(), x.rend()
  7. #define sz(x) ((int)x.size())
  8. #define ln(x) ((int)x.length())
  9. #define mp make_pair
  10. #define pb push_back
  11. #define ff first
  12. #define ss second
  13. #define endl '\n'
  14. #define dbg(x) cout << #x << ": " << x << endl
  15. #define clr(x,v) memset(x, v, sizeof(x))
  16. #define dblout(x) cout << setprecision(x) << fixed;
  17. #define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  18. using namespace std;
  19. const double eps = 1e-9;
  20. const double PI = acos(-1.0);
  21. const ll mod = 1e9 + 7;
  22. const int MAXN = 1e4 + 5;
  23. const ll inf = INT_MAX;
  24.  
  25. vector<vector<pi>> adj;
  26. bool hotel_at[MAXN], vis[MAXN][605];
  27. int n;
  28. ll dp[MAXN][605];
  29.  
  30. ll dfs(int u, int travel)
  31. {
  32.     // Destination
  33.     if(u == n)
  34.         return 0;
  35.  
  36.     // Visited state
  37.     if(vis[u][travel])
  38.         return dp[u][travel];
  39.  
  40.     vis[u][travel] = 1;
  41.     ll &ans = dp[u][travel];
  42.     ans = inf;
  43.  
  44.     for(auto nx : adj[u])
  45.     {
  46.         int v = nx.ff, w = nx.ss;
  47.         // if hotel at u, try sleeping at the hotel and increase the day
  48.         if(hotel_at[u])
  49.             ans = min(ans, 1 + dfs(v, 600 - w));
  50.         // try to go directly without resting
  51.         if(travel >= w)
  52.             ans = min(ans, dfs(v, travel - w));
  53.     }
  54.  
  55.     return ans;
  56. }
  57.  
  58. void cp()
  59. {
  60.     int h, m;
  61.     while(cin >> n && n)
  62.     {
  63.         adj.clear();
  64.         adj.resize(n + 1);
  65.         fill(hotel_at, hotel_at + n + 1, 0);
  66.  
  67.         cin >> h;
  68.         for(int i = 0, x; i < h; i++)
  69.             cin >> x, hotel_at[x] = 1;
  70.  
  71.         cin >> m;
  72.         for(int i = 0, u, v, w; i < m; i++)
  73.             cin >> u >> v >> w, adj[u].pb({v, w}), adj[v].pb({u, w});
  74.  
  75.         // Clear DP and vis
  76.         for(int i = 0; i <= n; i++)
  77.             for(int j = 0; j <= 600; j++)
  78.                 dp[i][j] = vis[i][j] = 0;
  79.  
  80.         ll ans = dfs(1, 600);
  81.         cout << (ans < inf ? ans : -1) << endl;
  82.     }
  83. }
  84.  
  85. int main()
  86. {
  87.     FASTIO;
  88.     int t;
  89.     t = 1;
  90.     // cin >> t;
  91.     while(t--)
  92.     {
  93.         cp();
  94.     }
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment