Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define F first
  4. #define S second
  5. #define pb push_back
  6. #define llong long long
  7. #define ld long double
  8. #define endl '\n'
  9. #define pii pair<int, int>
  10.  
  11. using namespace std;
  12.  
  13. vector <pair<int, pii>> g[50000];
  14. map <pii, int> e;
  15. bool f[50000];
  16. set <int> my;
  17. set <int> buy;
  18. int r[2010];
  19.  
  20.  
  21. int main() {
  22. ios_base::sync_with_stdio(0);
  23. cin.tie(0);
  24. cout.tie(0);
  25. srand(time(0));
  26. #ifdef LOCAL
  27. freopen("input.txt", "r", stdin);
  28. freopen("output.txt", "w", stdout);
  29. #else
  30. //freopen("input.txt", "r", stdin);
  31. //freopen("output.txt", "w", stdout);
  32. #endif // LOCAL
  33.  
  34. int n, m;
  35. cin >> n >> m;
  36. for (int i = 0; i < n; ++i){
  37. cin >> r[i];
  38. }
  39. int sum = 0;
  40. for (int i = 0; i < m; ++i){
  41. int x, y, h, c;
  42. cin >> x >> y >> h >> c;
  43. --x; --y;
  44. g[x].pb({y, {c + r[y], i}});
  45. g[y].pb({x, {c + r[x], i}});
  46. e[{x, y}] = i;
  47. e[{y, x}] = i;
  48. if (h == 1){
  49. sum += c;
  50. my.insert(i);
  51. }
  52. else f[i] = 1;
  53. }
  54.  
  55. //deikstra
  56. vector <int> dis(n, 1e9);
  57. vector <int> pr(n, 1e9);
  58. priority_queue<pii> q;
  59. q.push({0, 0});
  60. dis[0] = 0; pr[0] = 0;
  61. while (!q.empty()){
  62. int l = -q.top().F;
  63. int v = q.top().S;
  64. q.pop();
  65. if (l > dis[v]) continue;
  66. for (auto to : g[v]){
  67. int too = to.F;
  68. int len = to.S.F;
  69. if (dis[too] > dis[v] + len) {
  70. dis[too] = dis[v] + len;
  71. pr[too] = v;
  72. q.push({-dis[too], too});
  73. }
  74. }
  75. }
  76.  
  77. if (dis[n - 1] > sum){
  78. cout << -1;
  79. return 0;
  80. }
  81. int i = n - 1;
  82. vector <int> path;
  83. vector <int> way;
  84. while (i != pr[i]){
  85. path.pb(e[{i, pr[i]}]);
  86. way.pb(i);
  87. i = pr[i];
  88. }
  89. way.pb(i);
  90. reverse(path.begin(), path.end());
  91. reverse(way.begin(), way.end());
  92. for (auto to : path){
  93. if (f[to] == 1){
  94. buy.insert(to);
  95. }
  96. else my.erase(to);
  97. }
  98. cout << my.size() << ' ';
  99. for (auto to : my){
  100. cout << to + 1 << ' ';
  101. }
  102. cout << endl;
  103.  
  104. cout << buy.size() << ' ';
  105. for (auto to : buy){
  106. cout << to + 1 << ' ';
  107. }
  108. cout << endl;
  109. for (auto to : way){
  110. cout << to + 1 << ' ';
  111. }
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement