Advertisement
mateuspl

uri1640

Apr 6th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define fi first
  4. #define se second
  5.  
  6. using namespace std;
  7.  
  8. typedef pair<int, int> ii;
  9. typedef vector<ii> vii;
  10.  
  11. int N, m, a, b, t, nh;
  12. int h[10001];
  13. vii G[10001];
  14. int ans[10001];
  15.  
  16. void dfs(int n, int sz, int ht)
  17. {
  18.  
  19. ans[n] = min(ans[n], ht);
  20. for (int i = 0; i < G[n].size(); i++) {
  21. if (ht < ans[G[n][i].fi])
  22. {
  23. if (G[n][i].se + sz <= 600)
  24. dfs(G[n][i].fi, G[n][i].se + sz, ht); //nao descanso
  25. if (h[n] == 1)
  26. dfs(G[n][i].fi, G[n][i].se, ht + 1); //descanso
  27. }
  28. else if (ht == ans[G[n][i].fi])
  29. {
  30.  
  31. }
  32. }
  33.  
  34. }
  35.  
  36.  
  37. int main()
  38. {
  39. while (cin >> N && N)
  40. {
  41. for (int i = 0; i < N; i++) {
  42. G[i+1].clear();
  43. ans[i+1] = 1 << 30;
  44. }
  45.  
  46. cin >> nh;
  47. memset(h, 0, sizeof h);
  48. for (int i = 0; i < nh; i++) {
  49. cin >> t; h[t] = 1;
  50. }
  51.  
  52. cin >> m;
  53. for (int i = 0; i < m; i++) {
  54. cin >> a >> b >> t;
  55. G[a].push_back(ii(b, t));
  56. G[b].push_back(ii(a, t));
  57. }
  58.  
  59. dfs(1, 0, 0);
  60. cout << (ans[N] == (1 << 30) ? -1 : ans[N]) << endl;
  61. }
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement