#P4510. [CTSC2015] 性别改造计划
[CTSC2015] 性别改造计划
Description
The 21st century is the century of life sciences. Humanity has invested extensive manpower and resources into life science research, aiming for a deeper understanding of the survival mechanisms of various organisms, so as to better understand ourselves and improve quality of life. The government of Country Q first launched gender modification research in sheep, hoping to enhance the survival ability of the entire sheep population by changing gender through gene recombination. If this plan succeeds, humans will master the black technology of arbitrarily changing animal gender, which will be an important milestone in the history of life science research.
The government of Country Q selects sheep from the flock as experimental samples. Among these sheep, there exist bloodline chains of length , each of a single gender. A “bloodline chain” is a chain consisting of parent–child relationships, e.g., “grandfather – father – son” is a bloodline chain. “Single gender” means that all individuals in each bloodline chain are of the same gender, either all male or all female. The length of a bloodline chain is the number of individuals in the chain, e.g., “grandfather – father – son” has length . These bloodline chains do not intersect.
Note that not every sheep must belong to a bloodline chain; some sheep might belong to none. Therefore, . Beyond the bloodline chains, there can also be “mating relationships” among same-generation sheep. Two opposite-sex sheep that have reproduced offspring form a “mating relationship.” Note that a mating relationship only occurs between opposite-sex individuals of the same generation. Here, “same generation” means they are of the same generation level, i.e., a sheep only forms mating relationships with its siblings’ generation, and never with its parents, children, or individuals of more distant generations.
Changing a sheep’s gender incurs a large experimental cost: changing the gender of sheep costs . Furthermore, changing genders affects the stability of mating relationships: for each mating relationship , there is an initial stability and an attenuation factor . After all gender modifications are completed, if neither side’s gender changed, the stability is ; if both sides’ genders were flipped, then ; in all other cases, .
Given each sheep’s gender, the gender change cost, all bloodline chains, and all mating relationships, Country Q asks you to design a gender modification plan to maximize the total profit. The profit is computed as follows:
Here is the number of adjacent pairs within the bloodline chains that are of opposite gender after modification, is the sum of the stabilities of all mating relationships after modification, i.e., , and is the total cost of gender changes, i.e., .
Input Format
- The first line contains four non-negative integers , , , , denoting the length of each bloodline chain, the number of bloodline chains, the total number of sheep in the sample, and the number of mating relationships, respectively.
- The second line is a string of characters, each being
MorF, describing the initial gender of the sheep.Mstands for male,Fstands for female. - The third line contains positive integers , the cost to change the gender of each sheep.
- The next lines each contain integers, giving the indices (from to ) of the sheep in each of the bloodline chains. It is guaranteed that each chain is single-gender and that the chains are disjoint.
- The next lines each contain three integers , , and a real number , indicating that sheep and sheep have a mating relationship with initial stability and attenuation factor .
It is guaranteed that the initial genders of and are different, and are of the same generation, and each relationship appears at most once in the input.
Output Format
Output a single integer on one line, the maximum achievable profit under the modification plan.
3 2 6 2
MMMFFF
10000 200 10 10000 200 10
1 2 3
4 5 6
2 5 20 0.1
3 6 20 0.9
360
Hint
Sample explanation:
Change genders to MMFFFM. The profit is $\lfloor 10 \ln(1 + 2) \rfloor \times (20 + 18) - (10 + 10) = 360$. Here because in bloodline chain , sheep and are of different genders, and in bloodline chain , sheep and are of different genders.
Constraints:
- For of the testdata, .
- For of the testdata, .
- For of the testdata, . These three categories are pairwise disjoint.
- For of the testdata, , , , , , , and the number of decimal places of does not exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号