HDU 4799-LIKE vs CANDLE-动态规划-[解题报告]HOJ
A microblog caused a war recently – There’s the war between LIKE and CANDLE.
As you see, there are N accounts are trying to show their support of LIKE or CANDLE. The way they show the support is forwarding a microblog, which means you choose someone’s microblog and repost it with some comment.
A valid support microblog is forwarding the original account’s microblog or a other valid support microblog. We can assume that all accounts will forward the microblog only once. Also, it is impossible for a microblog forwarding a microblog that posts after
When the activity ends, someone will use a software to check these accounts and calculate a Power Point for LIKE and CANDLE. Specifically, each account will have a value based on some algorithm (you need not to care). The
value will be added to LIKE if the account is voting LIKE, vice versa. So easy, isn’t it?
Edward is a programmer and he supports LIKE. He found a bug in the software that used in the activity – He can spend XPower Point of LIKE to flip an account. When an account
is flipped, it will be seen as it votes the other side. For example, if Alice votes LIKE and then it is flipped, the software will add the value to CANDLE. Of course, an account can be flipped for several times – If Alice is flipped again, it votes for LIKE
again. And if we called the account the flipped account (Notice it’s only a concept indicates the account has been flipped and not an attribute of an account), all accounts which forwarding the flipped account‘s
microblog will also be flipped.
Soon, Edward found that someone uses this bug before! Some accounts have been flipped already. He can’t spend XPower Point to flip them anymore; instead, he need spend YPower Point to flip an
account which has been flipped directly by someone.
For the glory of the LIKE, please help Edward to flip accounts so that the Power Point of LIKE can be larger than CANDLE as much as possible.
You can spend Power Point as much as you like, no matter the total Power Point of LIKE is negative or not.
The input contains no more than 20 test cases. Notice there’s no empty line between each test case.
For each test case, first line has three integers N (1 ≤ N ≤ 50000) – the number of the accounts, X (0 ≤ X ≤
1000) and Y (0 ≤ Y ≤ 1000) – as the problem description. The account is numbered from 1 to N and 0 represent the original account.
Following N lines, the ith line means the ith account. Each line has four integers: V (0
≤ V ≤ 1000) – the value of the ith account, F (0 ≤ F ≤ N) – which account
did the ith account’s forwarding account come from (0th microblog is original account’s microblog), S (0 ≤ S ≤
1) – the status of flipped (0 means no changed, 1 means changed) and P (0 ≤ P ≤ 1) – the side the account supports without flipped (0 means LIKE, 1 means CANDLE).
The original microblog’s account can’t be flipped, and it hasn’t the value and the support side.
For each test case print an integer, represents the maximum result of the value of LIKE minus the value of CANDLE. If the value of CANDLE is larger than the LIKE, then just output "HAHAHAOMG" (without quote).
4 3 2
5 0 0 0
3 1 0 1
4 2 1 0
1 2 0 0
输入：N个账户，X flip一个没有fliped的账户需要的花费，Y flip一个已经fliped的账户需要的花费。
思路：DP，dp[i][0/1]表示是 第i个账户 是否被flip之后 的最大价值。
转移方程：dp[i] += max(dp[j],f[j] – (flip[j]?Y:X));
dp[i] += max(dp[j],f[j] – (flip[j]?Y:X));
using namespace std;
#define maxn 50050
void solve(int u)
vis[u] = 1;
if(s[u])state ^= 1;
if(state)v[u] = -v[u];
dp[u] = v[u];
dp[u] = -v[u];
for (int i = head[u]; i!=-1; i = edge[i].nxt)
int y = edge[i].t;
dp[u] += max(dp[y],dp[y]-(s[y]?Y:X));
dp[u] += max(dp[y],dp[y]-(s[y]?Y:X));
if(s[u]) state ^= 1;
int ind = 0;
state = 0;
for (int i = 1; i <= n; i++)
if(p) v[i] = -v[i];
edge[ind].t = i;
edge[ind].nxt = head[f];
head[f] = ind++;
if(dp < 0)