Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <queue>
- #include <set>
- #include <cstring>
- #include <map>
- #include <bitset>
- #include <random>
- #include <stack>
- #include <list>
- using namespace std;
- #define ll long long
- #define sc second
- #define fs first
- #define mp make_pair
- #define pb push_back
- int n, cost[411111], prv[411111];
- vector<int> g[411111];
- ll dfs(int v)
- {
- ll go_up = (ll)1e18, go_with = -1;
- for (int i = 0; i < g[v].size(); i++)
- {
- int get = dfs(g[v][i]);
- if (get < go_up)
- go_up = get, go_with = g[v][i];
- }
- if (go_up == (ll)1e18)
- go_up = 0;
- prv[v] = go_with;
- return (cost[v] + go_up);
- }
- int main()
- {
- //FILE *f;
- //freopen_s(&f, "in.txt", "r", stdin);
- //freopen_s(&f, "out.txt", "w", stdout);
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- cin >> n;
- memset(cost, 0, sizeof(cost));
- memset(prv, 255, sizeof(prv));
- for (int i = 0; i < n; i++)
- {
- int v, k;
- cin >> v >> k;
- while (k--){
- int u, cst;
- cin >> u >> cst;
- cost[u] = cst;
- g[v].push_back(u);
- }
- }
- cout << dfs(1) << endl;
- int cur = min(n, 1);
- while (cur != -1){
- cout << cur << ' ';
- cur = prv[cur];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement