Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <list>
- #include <string>
- #include <functional>
- #include <algorithm>
- #include <climits>
- using namespace std;
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- int n;
- fin >> n;
- vector<list<int>> vec(n);
- vector<int> cost(n);
- vector<bool> used(n);
- vector<vector<int>> dp(2, vector<int>(n, INT_MAX));
- string str;
- getline(fin, str);
- for (int i = 0; i < n; i++) {
- getline(fin, str);
- int a; int prev = 0;
- for (int j = 0; j < str.size(); j++)
- if (str[j] == ' ' || j == str.size() - 1) {
- vec[i].emplace_back(atoi(str.substr(prev, 2*str.size() - prev - j).c_str())-1);
- prev = j + 1;
- }
- }
- for (int i = 0; i < n; i++) fin >> cost[i];
- function<void(int)> dfs = [&](int curV) {
- used[curV] = 1;
- int sum0 = 0, sum1 = 0;
- for (int to : vec[curV])
- if (!used[to]) {
- dfs(to);
- sum0 += dp[1][to];
- sum1 += min(dp[1][to], dp[0][to]);
- }
- dp[0][curV] = min(dp[0][curV], sum0);
- dp[1][curV] = min(dp[1][curV], sum1 + cost[curV]);
- };
- dfs(0);
- fout << min(dp[0][0], dp[1][0]) << endl;
- for (int i = 0; i < n; i++)
- if (dp[1][i] < dp[0][i])
- fout << i+1 << " ";
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment