Python group of the 11th Blue Bridge Cup provincial competition in 2020 (real topic + analysis + code): Crop hybridization

Posted by curmudgeon42 on Fri, 11 Feb 2022 11:59:35 +0100

1 true question

Title Description

Crop hybridization is an important step in crop cultivation. There are known n crops (No. 1 to n), and the time from sowing to maturity of the ith crop is Ti. Crops can be hybridized in pairs, and the longer of the two kinds of time is taken as the hybridization time. If the planting time of crop A is 5 days and that of crop B is 7 days, the time of AB hybridization is 7 days. Crop hybridization will produce fixed crops, and the newly produced crops still belong to one of N crops.
At the beginning, there are seeds of M crops (unlimited quantity, which can support multiple crosses). Multiple hybridization processes can be carried out at the same time. Ask how many days it takes to get a given target seed.
If there are four kinds of crops ABCD, their maturity time is 5 days, 7 days, 3 days and 8 days. Initially, there are seeds of AB crops, the target seed is D, and the known hybridization condition is a × B → C,A × C → D. The shortest hybridization process is:
Day 1 to 7 (time of crop b), A × B → C.
Day 8 to 12 (time of crop A), A × C → D.
It takes 12 days to get the seeds of crop D.

Enter description

The first line of the input contains four integers, N, M, K, T, n represents the total number of crop species (numbers 1 to N), M represents the number of crop seed types initially owned, K represents the number of schemes that can be hybridized, and T represents the number of target seeds.
The second line contains N integers, where the ith integer represents the planting time ti of the ith crop (1 < = Ti < = 100)
The third line contains M integers, which respectively represent the seed type Kj (1 < = Kj ≤ M), and Kj is different.
Lines 4 to K+ 3, each line contains three integers A, B and C, indicating that the seeds of category C crops can be obtained by crossing category A crops and category B crops.
Where n < = 1, n < = 1, n < = 1, n < = 2
Ensure that the target seed can be obtained by hybridization.

Output description

Output an integer indicating the shortest hybridization time to obtain the target seed.

sample input

6 2 4 6
5 3 4 6 4 9
1 2
1 2 3
1 3 4
2 3 5
4 5 6

sample output


Example description

From day 1 to day 5, the crops No. 1 and No. 2 were hybridized to obtain the crop seeds No. 3.
From day 6 to day 10, the crops No. 1 and No. 3 were hybridized to obtain the crop seeds No. 4.
From day 6 to day 9, the crops No. 2 and No. 3 were hybridized to obtain the crop seeds No. 5.
From day 11 to day 16, the crops No. 4 and No. 5 were hybridized to obtain the crop seeds No. 6.
It takes 16 days in total.

2 Analysis

Degree of Difficulty: ⭐⭐⭐

Investigation question type: graph theory dynamic programming

Knowledge points involved: DFS DP

Train of thought analysis:

Step 1: review the questions. I watched this topic for 10 minutes before I knew exactly what crop hybridization was doing.

Step 2: input. Input various conditions in the title. It is better to write sample input next to it, which is more convenient to understand and will not be dizzy.

Step 3: search. Recurs the known crop from the target crop and write a dfs function.

3 code

Detailed annotation version

#Crop hybridization dfs
n,m,k,t=map(int,input().split())#n: Seed number - Category 6 m: initial seed number - 2 k: hybridization scheme - 4 species t: target seed number - 6
T=list(map(int,input().split()))#Planting time T=[5,3,4,6,4,9]
M=list(map(int,input().split()))#Owned seed type M=[1,2]

mix=[[] for i in range(n+1)]#The hybridization list is initialized to 0
for i in range(k):#Input 4 hybridization schemes
    temp1,temp2,temp3=map(int,input().split())#Hybridization scheme#1+2→3 #1+3→4 #2+3→5 #4+5→6
    temp=max(T[temp1-1],T[temp2-1])#Maximum hybridization time#5 5 4 6
    mix[temp3].append([temp1,temp2,temp])#[1,2,5] [1,3,5] [2,3,4] [4,5,6]
    #mix=[[], [], [], [[1, 2, 5]], [[1, 3, 5]], [[2, 3, 4]], [[4, 5, 6]]]
    #[[[], [], []]] this format is because there may be multiple hybridization schemes for a crop
f=[100000 for i in range(n+1)]#Maximum initialization time of crop culture
for i in M:#Initializing the existing seed culture time is 0
def dfs(get):#Time required to obtain the target crop
    if f[get]!=100000:#The calculated crop is returned directly
        return f[get]
    for i in range(len(mix[get])):#Traverse all hybridization schemes of a crop
            #dfs(1)=0 #dfs(2)=0  
    return f[get]


Reduced code version

mix=[[] for i in range(n+1)]
for i in range(k):
f=[100000 for i in range(n+1)]
for i in M:
def dfs(get):
    if f[get]!=100000:
        return f[get]
    for i in range(len(mix[get])):
    return f[get]

Topics: Python Algorithm Graph Theory