Advertisement
Guest User

Untitled

a guest
Aug 9th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <algorithm>
  7. #include <fstream>
  8. #include <cmath>
  9.  
  10. #define ll long long
  11. #define pii pair<int,int>
  12. #define pll pair<long long, long long>
  13. #define mp(a, b) make_pair(a, b)
  14.  
  15.  
  16. using namespace std;
  17.  
  18. #define cin in
  19. #define cout out
  20.  
  21. ifstream in("dwarf.in");
  22. ofstream out("dwarf.out");
  23.  
  24. ll a[1234567];
  25.  
  26. struct p
  27. {
  28. ll x, a, b, c;
  29. p(ll xx, ll aa, ll bb, ll cc)
  30. {
  31. x = xx;
  32. a = aa;
  33. b = bb;
  34. c = cc;
  35. }
  36. };
  37.  
  38.  
  39. bool operator<(p aa, p bb)
  40. {
  41. return (a[aa.a] + a[aa.b] + 1)*9999999ll + aa.c < (a[bb.a] + a[bb.b] + 1)*9999999ll + bb.c;
  42. }
  43.  
  44. vector<vector<p> > vc;
  45.  
  46. int main()
  47. {
  48. set<p> st;
  49. int n, m;
  50. cin >> n >> m;
  51. vc.resize(n+1);
  52. for(int i = 1; i <= n; ++i)
  53. {
  54. cin >> a[i];
  55. }
  56. p t(0,0,0,0);
  57. for(int i = 0 ; i < m; ++i)
  58. {
  59. cin >> t.x >> t.a >> t.b;
  60. t.c = i + 1;
  61. st.insert(t);
  62. vc[t.a].push_back(t);
  63. vc[t.b].push_back(t);
  64. }
  65.  
  66. while(st.size())
  67. {
  68. p pp = *(st.begin());
  69. st.erase(pp);
  70. if(a[pp.a] + a[pp.b] < a[pp.x])
  71. {
  72. a[pp.x] = a[pp.a] + a[pp.b];
  73. if(pp.x == 1)
  74. {
  75. cout << a[1];
  76. return 0;
  77. }
  78. for(unsigned int j = 0; j < vc[pp.x].size(); ++j)
  79. if(a[vc[pp.x][j].a] + a[vc[pp.x][j].b] < a[vc[pp.x][j].x])
  80. if(st.find(vc[pp.x][j]) != st.end())
  81. {
  82. st.erase(vc[pp.x][j]);
  83. st.insert(vc[pp.x][j]);
  84. }
  85. }
  86.  
  87. }
  88. cout << a[1];
  89. return 0;
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement