Computer Science, asked by leoishere, 1 year ago

the shortest distance between two vertices A and B in the graph
the shortest distance between two vertices A and B in the graph. If there are multiple
shortest paths between A and B, you have to nd the path with the least number of
edges.
Input Format
The vertices are represented by numbers starting from one. The rst line will include two
numbers N and M, the number of vertices in the graph and the number of edges in the
graph respectively. The following M lines will have three numbers each, u, v, w which
represents adirected edge between u and v with a weight w. The nal line will have the
values of A and B, the nodes between which you will have to nd the shortest path.
Output Format
Print two numbers separated by a space: C E, where C is the cost from A to B and E is
the number of edges from A to B. For unreachable nodes, print -1 -1, denoting no valid
distance and no valid number of edges, i.e. C = -1 and E = -1.

Constraints
Consider the fact the input graph can be quite large. You should come up with e cient
code to avoid the timeout error. However, there is a limit on the number of vertices and
number of vertices as follows.
1 <= M <= 104
1 <= N <= 104

Answer can be in C c++ ,java, phython only

Answers

Answered by Jyoti1711
0
The shortest distance between two vertices A and B in the graph. If there are multiple shortest paths between A and B, you have to nd the path with the least number of edges.

Input Format The vertices are represented by numbers starting from one. The rst line will include two numbers N and M, the number of vertices in the graph and the number of edges in the graph respectively. The following M lines will have three numbers each, u, v, w which represents a directed edge between u and v with a weight w. The nal line will have the values of A and B, the nodes between which you will have to nd the shortest path.

Output Format Print two numbers separated by a space: C E, where C is the cost from A to B and E is the number of edges from A to B. For unreachable nodes, print -1 -1, denoting no valid distance and no valid number of edges, i.e. C = -1 and E = -1.

Example 1 Consider the following graph. It has ve vertices and ve edges. We have to nd the shortest part from vertex 1 to vertex 5. There are two shortest paths, based on the weight of the path, from vertex 1 to vertex 5: path 1,2,5 and path 1,3,4,5. But we want to end the path with minimum number of edges. So we pick the path with total weight 3 and number of edges 2.

1 2 3 4 5 1 2 1 1 1

Input for Example 1

5 5 1 2 1 1 3 1 2 5 2 3 4 1 4 5 1 1 5

Output for Example 1

3 2

Constraints

Consider the fact the input graph can be quite large. You should come up with the client code to avoid the timeout error. However, there is a limit on the number of vertices and number of vertices as follows.

1 <= M <= 104 1 <= N <= 104

I have try the following but it is throwing error also not getting desire result what I am doing wrong. even I think i am using code.

int main(){ double c; double d; double a; double b; double answer; cout << "Enter the points for the coordinates"; cout << endl; cout << "Point C for first coordinates: "; cin >> C; cout << endl; cout << endl; cout << "Point d for first coordinate: "; cin >> d; cout << endl; cout << endl; cout << "Point C for the second coordinate: "; cin >> a; cout << endl; cout << endl; cout << "Point D for the second coordinate: "; cin >> b; cout << endl; cout << endl; answer = distanceBetweenTwoPoints(c, d, a, b); cout << "The answer is " << answer; }
Similar questions