我正在尝试解决需要图表表示的问题。从文件中提取图形节点和边缘信息。到目前为止,我已经为节点实现了一个简单的结构:
struct Node {
unsigned long long id;
vector< pair<Node*, unsigned long long> > paths;
};
添加边的功能:
void add_path (vector<Node> &graph, unsigned long long id_1, unsigned long long id_2, unsigned long long distance){
graph[id_1].paths.push_back({&graph[id_2], distance});
}
按照问题要求的方式初始化图形的功能:
vector<Node> generate_graph(unsigned long long n, vector<Node> &graph){
graph[1].paths.push_back({&graph[2], 0});
for(unsigned long long i = 2; i < graph.size(); ++i){
graph[i].id = i;
graph[i].paths.push_back({&graph[i+1], i});
graph[i].paths.push_back({&graph[i-1], 0});
}
return graph;
}
一个主函数,它将从文件中读取值,并为每个案例生成适当的图形。
int main(){
ifstream file_input ("5.input", ifstream::in);
ofstream file_output ("5.output", ofstream::out);
int n_cases;
file_input >> n_cases;
for(int cas = 1; cas <= n_cases; ++cas){
unsigned long long n_levels;
file_input >> n_levels;
cout << "starting case: " << cas << " --> " << n_levels << endl;
vector<Node> graph(n_levels+2);
generate_graph(n_levels, graph);
unsigned long long shortcuts;
file_input >> shortcuts;
unsigned long long id_1, id_2, distance;
for(unsigned long long n_short = 0; n_short < shortcuts; ++n_short){
file_input >> id_1 >> id_2 >> distance; // <--- here seems to be the problem
add_path(graph, id_1, id_2, distance);
}
//file_output << "Case #" << cas << ": " << a_star(graph, 1, n_levels) << endl;
}
file_input.close();
file_output.close();
}
5.input包含大约800个不同的案例。其中一个案例有1013719052个节点和1943个边缘(快捷循环)。当程序到达这一点时,它只是一直在思考。我的猜测是有一种更有效的方法来读取输入值,这将解决我的问题。
对不起我的英文并提前致谢。
编辑:
似乎问题是在生成图表时,太多节点会使进程变慢。