The third question of Huawei's recent written test (9.8)

Keywords: C++ data structure

I didn't take part in these computer-based exams. I was interested in doing it. Interested students can refer to it
Question 3 (300 points) minimum compilation time
Title Description:
Company A needs to introduce an open source project into the project and evaluate the compilation time of A module in the open source project. At present, the compilation time of each module in the project and the list of modules it depends on are known. When there are an unlimited number of parallel tasks, find the shortest compilation time of A specified module.
If there is a circular dependency between modules or the dependent module does not exist, the compilation cannot be completed and - 1 is returned.
Enter Description:
The first line input is the name of the target module, and then each line input defines a module, including the module name, compilation time and dependent module list, separated by commas. If the dependent module list does not exist, it means that it can be compiled independently, for example:
The module name contains only letters and numbers and at least one character
The number of modules shall not exceed 50000
Output Description:
Output the minimum compilation time. If the compilation cannot be completed, output - 1
Example 1:
Output: 20
Note: it takes 10ms to compile module1, 5ms to compile module2, and 10ms to compile module3. Therefore, the total compilation time is 10+max(10,5) = 20ms
Example 2:
Output: - 1
Note: module1 is undefined and cannot be compiled, so - 1 is output
Reference code:

//9.8 Huawei question 3
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>

using namespace std;

struct node {
	string key;
	int value;
	vector<node>* from = nullptr;
	node(string k, int v) :key(k), value(v)
		from = new vector<node>();
	bool operator < (const node& a)const
		return key < a.key;

int main()
	map<string, node> hstable;   //chart
	string target;
	cin >> target;
	string line;
	vector<vector<string>> vec;
	while (getline(cin,line))
		string temp;
		if (line == "")continue;
		if (line == "e")break;      //Exit. Your own test example cannot jump out of this loop and will wait for input
		stringstream ss(line);
		vector<string> s_temp;
		while (getline(ss, temp, ','))
	}  //Complete all data entry
	cout << vec.size() << endl;
	for (int i = 0; i < vec.size(); i++)   //Let's go first
		if (hstable.find(vec[i][0]) == hstable.end())   //If not, add a new one
			hstable.emplace(vec[i][0], node(vec[i][0], atoi(&vec[i][1][0])));
	for (int i = 0; i < vec.size(); i++)
		if (vec[i].size() != 2)   //Complete dependency establishment
			for (int j = 2; j < vec[i].size(); j++)
				if (hstable.find(vec[i][j]) == hstable.end())   //Undefined, unable to complete compilation
					cout << -1 << endl;
					return 0;
	set<node> finish;
	int ans = 0;
	queue<node> myqueue;
	myqueue.push(hstable.find(target)->second);                    //Target point
	while (!myqueue.empty())
		int len = myqueue.size();
		int temp_max = 0;
		for (int i = 0; i < len; i++)
			node temp = myqueue.front();
			temp_max = max(temp_max, temp.value);     //Keep the maximum value of each layer
			for (auto i : *temp.from)
				if (finish.find(i) != finish.end())   //Depending on the previous point, it indicates that there is a ring
					cout << -1 << endl;
					return 0;
				finish.emplace(i);                   //The point of completing the mission
		ans += temp_max;
	cout << ans << endl;
	return 0;

Summary: for the bfs of the graph, many examples are considered here. After weighing, a hash table is used to store the traversed point structure, while the method of topological sorting (there should be edges of other points, which depend on it) can not pass under several special examples. Here, I also do a reverse, pushing back from the target point and discarding the unwanted points.
Question:<sting,node> hstable; I use Hstable ["xxx"] to represent a node data structure, and the ide reports an error. There is no problem using hstable.find("xxx") - > second. If anyone knows what the problem is, please leave a comment.
2. My code runs under vs, so the input processing is a little different from the test. When calling getline(cin,line), you will find that an empty vector < string > will be pressed into the first position of vec. This may have something to do with my environment. I ran cin once before running this cycle, which will lead to problems.

Posted by blurredvision on Fri, 10 Sep 2021 12:48:26 -0700