- محدودیت زمان: ۱ ثانیه
- محدودیت حافظه: ۲۵۶ مگابایت
Leila is a Bioinformatician, interested in studying Bacterial evolution. In one experiment on a special type of Bacteria, she started from a single bacterium, put it on a plate, and monitored the bacterial division, until she obtained a population of bacteria. During the process, she carefully reported the evolutionary relations between bacteria. Precisely, for each bacterium, she reported its parent bacterium.
In the next step, she extracted DNA sequences of bacteria in the final population, by NGS technology. Each DNA sequence is represented as a string of length from the alphabet set .
The NGS technology has a drawback: it produces a lot of missing values. So, there are a lot of unknown characters indicated by ?
in the extracted sequences. Considering the evolutionary relationship between bacteria, Leila wants to impute the missing values. Among all possible imputations, she wants to find the minimum cost imputation from an evolutionary perspective.
The problem is defined as follows. A rooted tree is given, and for each leaf of , a string of length from the character set is given. A transition cost matrix is also given, where represents the cost of a transition from an character to a character, from a parent to its child.
A feasible imputation, assigns a string su of length from the character set to each vertex , where for each leaf of , is equal to except for ?
characters in . The evolutionary cost of an imputation is defined as the sum of evolutionary costs of all edges. The evolutionary cost of an edge between parent and child , is defined as
, where is the -th character of .
Leila wants to find a feasible imputation for , which has the minimum evolutionary cost among all feasible imputations. The tree , transition cost matrix , and a string for each leaf are given. You should write a program to compute the minimum evolutionary cost of feasible imputations.
ورودی
The first line of the input contains an integer () denoting the number of vertices of . The vertices of are numbered from to . The root of the tree is numbered . The root is never considered as a leaf, even if it has only one child. The next lines describe the edges of ; each line contains two endpoints of an edge separated by spaces. In the next four lines, the evolutionary cost matrix is given; each line is for one row of . Rows (corresponding to a parent) and columns (corresponding to a child) of are ordered to respectively represent characters and . All entries of are non-negative integers not more than . The next line just contains , the number of leaves. Finally, each leaf (its number) and its which is a string of size () appear in one line.
خروجی
In one line, print the minimum evolutionary cost of feasible imputations.
مثال
ورودی نمونه ۱
خروجی نمونه ۱
ارسال پاسخ برای این سؤال