Advertisement
Guest User

Untitled

a guest
Oct 13th, 2020
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define mod 1000000007
  6. #define pb push_back
  7. #define S second
  8. #define F first
  9. #define INF 1e18
  10. #define all(v) v.begin(), v.end()
  11.  
  12. #define deb(x) cerr << "\n" \
  13. << #x << "=" << x << "\n";
  14. #define deb2(x, y) cerr << "\n" \
  15. << #x << "=" << x << "\n" \
  16. << #y << "=" << y << "\n";
  17. #define w(x) \
  18. int x; \
  19. cin >> x; \
  20. while (x--)
  21.  
  22. const int N = 1e5 + 2;
  23. vector<pair<int,int>> v[N];
  24. int n,m;
  25. bool vis[N];
  26. vector<pair<int,int>> dp(N,{-1,-1});
  27.  
  28. pair<int,int> dfs(int x) {
  29. if(x == n)
  30. return {0,0};
  31.  
  32. if(dp[x].F != -1)
  33. return dp[x];
  34.  
  35. if(vis[x]) return {INF,INF};
  36.  
  37. int cost1 = INF,cost2 = INF;
  38. vis[x] = 1;
  39. for(pair<int,int> i: v[x]) {
  40. pair<int,int> temp = dfs(i.F);
  41. cost1 = min(cost1,temp.F + i.S);
  42. cost1 = min(cost1,temp.S + i.S / 2);
  43.  
  44. cost2 = min(cost2, temp.S + i.S);
  45. }
  46. vis[x] = 0;
  47.  
  48. // used = 1 or used = 0;
  49. return dp[x] = {cost1,cost2};
  50. }
  51. int32_t main() {
  52.  
  53. ios_base::sync_with_stdio(0);
  54. cin.tie(0);
  55. cout.tie(0);
  56. cin >> n >> m;
  57. for (int i = 0; i < m; i++) {
  58. int a, b, c;
  59. cin >> a >> b >> c;
  60. v[a].push_back({b,c});
  61. }
  62. cout << dfs(1).F;
  63. return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement