how to implement the Dijkstra algorithm?

this is my homework, it really strikes me.
I uploaded the pdf and the txt in the
https://hotfile.com/dl/190161205/25500c0/Dijkstra.tar.gz.html


hope you can help me, and give me some tips.

although I know the basic Dijkstra algorithm from the graphic, but I can't convert it to code.

I don't know how to compare the length if one node is selected, the optional

I have two txt files, the net_data.txt and the trip_data.txt
net_data.txt:
first line is the nodes, next line is the edges
and next each line : first 2 numbers are the node, and the next 4 numbers are 4 parameters to calculate the from node 1 to 2 's time.
trip_data.txt:
first line is the nodes, next line is the pairs
and next each line: first 2 numbers are the node, followed by the drives are in this edge

if you want to calculate the time form 2 nodes, you need 5 parameters, the 4 is in the net_data.txt, and the last is the drives amount in the the trip_data.txt


here's my bad code, I really don't know how to design the class, functions and data members, I'm so suck!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
.h
#include <vector>
#include <list>
#include <string>

using namespace std;

class Node   // I m not sure whether this is necessary
{
public:

private:
	Node* first;
	Node* previous;
};


class Edge             // I don't know how to design it
{                      // how many variablbs
public:
	Edge(char arg_origin, char arg_dest);
	void setOrigin(char value) : origin(value) {}
	void setDest(char value) : dest(value) {}
	void setParameter(double f, double b, double c, int p) : f(f), b(b), c(c), p(p) {}
private:
	char origin;
	char dest;
	double f;
	double b;
	double c;
	int p;
};


class Network            // this stucks me, too difficult for me
{
public:
	void read(string file1, string file2);
private:
	int nodes;
	int pairs;

	vector<int> agents;

	vector<Edge> network;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 .cpp
#include "Dijk.h"
#include <fstream>

void Network::read(string file1, string file2)
{
	Edge edge_temp;

	ifstream inFile;
	inFile.open(file1.c_str());
	inFile >> nodes;
	inFile >> pairs;

	for(int i = 0; i < pairs; ++i)
	{
		char first, second;

		inFile >> first;
		edge_temp.setOrigin(first);

		inFile >> second;
		edge_temp.setDest(second);

		double f, b, c;
		int p;
		inFile >> f;
		inFile >> b;
		inFile >> c;
		inFile >> p;
		edge_temp.setParameter(f, b, c, p);

		network.push_back(edge_temp);
	}


}
Last edited on
Topic archived. No new replies allowed.