Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <set>
  8. #include <cstring>
  9. #include <map>
  10. #include <bitset>
  11. #include <random>
  12. #include <stack>
  13. #include <list>
  14.  
  15. using namespace std;
  16.  
  17. #define ll long long
  18. #define sc second
  19. #define fs first
  20. #define mp make_pair
  21. #define pb push_back
  22.  
  23. int n, cost[411111], prv[411111];
  24. vector<int> g[411111];
  25.  
  26.  
  27. ll dfs(int v)
  28. {
  29. ll go_up = (ll)1e18, go_with = -1;
  30. for (int i = 0; i < g[v].size(); i++)
  31. {
  32. int get = dfs(g[v][i]);
  33. if (get < go_up)
  34. go_up = get, go_with = g[v][i];
  35. }
  36. if (go_up == (ll)1e18)
  37. go_up = 0;
  38. prv[v] = go_with;
  39. return (cost[v] + go_up);
  40. }
  41.  
  42. int main()
  43. {
  44. //FILE *f;
  45. //freopen_s(&f, "in.txt", "r", stdin);
  46. //freopen_s(&f, "out.txt", "w", stdout);
  47. freopen("in.txt", "r", stdin);
  48. freopen("out.txt", "w", stdout);
  49. cin >> n;
  50. memset(cost, 0, sizeof(cost));
  51. memset(prv, 255, sizeof(prv));
  52. for (int i = 0; i < n; i++)
  53. {
  54. int v, k;
  55. cin >> v >> k;
  56. while (k--){
  57. int u, cst;
  58. cin >> u >> cst;
  59. cost[u] = cst;
  60. g[v].push_back(u);
  61. }
  62. }
  63. cout << dfs(1) << endl;
  64. int cur = min(n, 1);
  65. while (cur != -1){
  66. cout << cur << ' ';
  67. cur = prv[cur];
  68. }
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement