Blue Bridge Cup-Crop Hybridization (C++)

topic description

Crop hybridization is an important step in crop cultivation. It is known that there are NN crops (numbered 11 to NN ), and the time from sowing to maturity of the ii crop is T_iTi​. Crops can be hybridized in pairs, and the hybridization time is the longer one of the two species. For example, the planting time of crop A is 5 days, and the planting time of crop B is 7 days, then the time for AB hybridization is 7 days. Crop hybridization will produce fixed crops, and the newly generated crops still belong to one of the NN crops.

At the beginning, there are seeds of MM crops (the number is unlimited, and multiple crosses can be supported). Multiple hybridization processes can be performed simultaneously. Ask how many days are needed to obtain a given target seed.

If there are 4 kinds of crops ABCD, their respective maturity times are 5 days, 7 days, 3 days, and 8 days. Initially there are seeds of two crops AB, the target seed is D, and the known hybridization situation is A × B → C, A × C → D. Then the shortest hybridization process is:

From day 1 to day 7 (the time of crop B), A × B → C.

Day 8 to Day 12 (the time of crop A), A × C → D.

It takes 12 days to get the seeds of crop D.

enter description

The first line of input contains 4 integers N, M, K, TN, M, K, T, NN represents the total number of crop types (number 11 to NN), MM represents the number of crop seed types initially owned, and KK represents hybrid The number of schemes, TT represents the number of the target seed.

Line 2 contains NN integers, where the ii-th integer represents the planting time of the ii-th crop T_i\ (1 \leq T_i \leq 100)Ti​ (1≤Ti​≤100).

Line 3 contains MM integers, respectively representing the owned seed type K_j\ (1 \leq K_j \leq M)Kj​ (1≤Kj​≤M), K_jKj​ is different in pairs.

Lines 4 to KK + 3, each line contains 3 integers A, B, CA, B, C, which means that the seeds of type CC crops can be obtained by crossing type AA crops with type BB crops.

Among them, 1 \leq N \leq 2000, 2 \leq M \leq N, 1 \leq K \leq 10^5, 1 \leq T \leq N1≤N≤2000,2≤M≤N,1≤K≤ 105,1≤T≤N, guarantee that the target seed can be obtained through crossing.

output description

Output an integer representing the shortest hybridization time to get the target seed.

Input and output samples

example

enter

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

output

16

 

Problem-solving ideas:

First of all, one thing to note: there may be more than one hybridization scheme to produce a certain seed . I didn't consider this at the beginning, so none of the test data passed.

Explain with the example given in the title: if we want to get the No. 6 seed, we must first get the No. 4 seed and the No. 5 seed; so the problem is decomposed into finding the shortest hybridization time between the No. 4 seed and the No. 5 seed; then, we To get the No. 4 seed, you must first get the No. 1 seed and the No. 3 seed. Here, the No. 1 seed already exists at the beginning, so we record the shortest hybridization time of the No. 1 seed as 0; to get the No. 3 seed, you must first Get No. 1 seed and No. 2 seed, and the shortest hybridization time of these two seeds already exists (both here are 0), so the calculation (the planting time is used in the calculation) gets the shortest hybridization time of No. 3 seed; this At that time, the minimum hybridization time of No. 1 seed and No. 3 seed has already been obtained, so we can calculate the shortest hybridization time of No. 4 seed; the same is true for No. 5 seed, and finally the shortest hybridization time of No. 6 seed can be calculated.

The ideas of recursion and backtracking are used here . In addition, for a certain seed, if the shortest hybridization time is obtained, we will save it, which can avoid subsequent redundant calculations and greatly save time.

Emphasize once again: in the above example, there is only one hybridization scheme to obtain seeds 3, 4, 5, and 6, but in fact, there can be many kinds. When solving the problem, each scheme should be considered, and the shortest time should be taken that.

code:

# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

const int maxn = 2010;
int N, M, K, T;
vector<int>times(maxn);     //保存种子的种植时间
vector<int>early(maxn, -1); //保存种子的最短杂交时间(初始化为-1)
vector<int>part[maxn];      //保存产生种子i的杂交方案(很可能大于一种)
int DFS(int target);

int main()
{
	cin >> N >> M >> K >> T;
	for (int i = 1; i <= N; i++) {
		cin >> times[i];
	}
	int m;
	for (int i = 1; i <= M; i++) {
		cin >> m;
		early[m] = 0;   //对于原本就有的种子,最短杂交时间设为0
	}
	int a1, a2, a3;
	for (int i = 1; i <= K; i++) {
		cin >> a1 >> a2 >> a3;
		//每两个数为一组杂交方案
		part[a3].push_back(a1);
		part[a3].push_back(a2);
	}
	int ans = DFS(T);
	cout << ans << endl;
	return 0;
}

int DFS(int target)
{
	int a, b;
	early[target] = 1e9;
	//杂交方案可能不止一种,要求出所有方案,再选择最优的
	for (int i = 0; i + 1 < part[target].size(); i += 2) {
		//a,b两个数构成一个杂交方案
		a = part[target][i];
		b = part[target][i + 1];
		if (early[a] == -1) {
			//求a的最短杂交时间
			early[a] = DFS(a);
		}
		if (early[b] == -1) {
			//求b的最短杂交时间
			early[b] = DFS(b);
		}
		//选择最优的杂交方案
		early[target] = min(early[target], max(times[a], times[b]) + max(early[a], early[b]));
	}
	return early[target];
}

The above is my view on this question, and I am very happy to share it with you.

Guess you like

Origin blog.csdn.net/CXR_XC/article/details/129970731