Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1.  
  2. #include <vector>
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <utility>
  8. #include <cstdlib>
  9.  
  10. using namespace std;
  11.  
  12. #define MAX 10001
  13.  
  14. int inside[MAX], outside[MAX], changes[MAX];
  15.  
  16. void main( void )
  17. {
  18. int n, m, x, k = 0;
  19.  
  20. cin >> n >> m;
  21.  
  22. for (int i = 0; i < m; i++)
  23. {
  24. cin >> inside[i] >> outside[i] >> changes[i];
  25. inside[i]--;
  26. outside[i]--;
  27. }
  28.  
  29. vector<int> L(n, INT_MIN), p(n, -1), way;
  30.  
  31. L[0] = 0;
  32.  
  33. for (int i = 0; i < n; i++)
  34. {
  35. x = -1;
  36. for (int j = 0; j < m; j++)
  37. if (L[inside[j]] > INT_MIN &&
  38. L[outside[j]] < L[inside[j]] + changes[j])
  39. {
  40. L[outside[j]] = L[inside[j]] + changes[j];
  41. p[outside[j]] = inside[j];
  42. x = 1;
  43. }
  44. }
  45.  
  46. for (int i = n - 1; i != -1; i = p[i])
  47. {
  48. k++;
  49. way.push_back(i);
  50.  
  51. if (k > n)
  52. break;
  53. }
  54.  
  55. if (L[n - 1] == INT_MIN)
  56. cout << ":(" << endl;
  57.  
  58. else if (x != -1 && (way.back() !=0 || way.front() != n - 1 || k > n))
  59. cout << ":)" << endl;
  60.  
  61. else
  62. cout << L[n - 1] << endl;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement