Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i know the logic behind it, but i don't know how to apply it to finish my code to output 8.

The way to get 8 was to added up the shortest paths to each vertex in the graph. The shortest path in program 3 is the path with the least number of vertices. If there is a tie then you use the weight of the path as a tie breaker.

Can someone please help me to finish my code to output 8 after entering the sample input as showing below?
Input:

4 6

Women Conservatives Neocons Veterans

Trump Women 1

Trump Conservatives 1

Trump Neocons 5

Women Neocons 1

Neocons Veterans 5

Conservatives Veterans 1

**Output:**

8

**Here My code:**


C++
#include <iostream>
#include <vector>
#include <climits>
#include <cstdio>

using namespace std;

int min( int a,int b);
int getIndexOfVertex(string vertices[], int numberOfVertices,string name);
string extractMinVertex(bool visited[], string vertices[], int distanceFromStart[],int numberOfVertices);
void relaxEdges(string vertex, vector< vector<string> > &graph, vector< vector<int> > &weights, int distanceFromStart[], string vertices[], int);
void Dijkstra( int distanceFromStart[], string vertices[], vector< vector<string> > &graph, vector< vector<int> > &weights, int numberOfVertices );

bool pathExists( int u,int v, vector< vector<string> > &graph,int numberOfVertices,string vertices[]);
void dfs( int i, vector< vector<string> > &graph, bool visited[],int numberOfVertices,string vertices[] );

int main()
{
    ///freopen("input.txt","r",stdin);
    /// to store the number of vertices and the number of edges
    int numberOfVertices = 0;
    int numberOfEdges = 0;

    cin >> numberOfVertices;
    cin >> numberOfEdges;

    /// array to store the vertices. Number of vertices is 1 + numberOfVertices, since "Trump" is the first vertex
    numberOfVertices++;
    string vertices[ numberOfVertices];

    /// first vertex is Trump. From him, speeches have to be communicated to all groups
    vertices[0] = "Trump";

    for( int i=1; i<numberOfVertices; i++)
    {
        string name;
        cin >> name;

        vertices[i] = name;
    }

    /// graph is an adjacency list here. This graph vector stores the edges
    /// for each vertex, we store the list of vertices names, as a vector
    vector< vector<string> > graph( numberOfVertices );

    /// corresponding to the adjacency list, is this weight vector, that stores the weight of an edge
    vector< vector<int> > weights( numberOfVertices );

    for( int i=0; i<numberOfEdges; i++)
    {
        string u,v;
        int weight;

        cin >> v >> u >> weight;

        int uIndex = getIndexOfVertex( vertices, numberOfVertices, u );
        graph.at(uIndex).push_back( v );
        weights.at(uIndex).push_back( weight );
    }

    /*for (int i=0; i<numberOfVertices; i++)
    {
        cout << "Vertex " << vertices[i] << " : ";
        for( int j=0; j<graph[i].size(); j++)
            cout << graph[i][j] << " , ";

        cout << endl;
        cout << "Vertex " << vertices[i] << " : ";
        for( int j=0; j<weights[i].size(); j++)
            cout << weights[i][j] << " , ";
        cout << endl;
    }*/

    /// an array storing the distanc of vertices from the start
    int distanceFromStart[ numberOfVertices ];
    distanceFromStart[0] = 0;

    for (int i=1; i<numberOfVertices; i++)
        distanceFromStart[i] = INT_MAX;

    Dijkstra( distanceFromStart, vertices, graph, weights, numberOfVertices);
    int distance = 0;

    for( int i=1; i<numberOfVertices; i++)
        distance += distanceFromStart[i];

    cout << distance << endl;
    return 0;

}

/*bool pathExists( int u,int v, vector< vector<string> > &graph, int numberOfVertices, string vertices[])
{
    bool visited[ numberOfVertices ];
    for( int i=0; i<numberOfVertices; i++)
        visited[i] = false;

    dfs( u,graph,visited,numberOfVertices );

    return visited[v];
}
void dfs( int i, vector< vector<string> > &graph, bool visited[],int numberOfVertices , string vertices[])
{
    visited[i] = true;

    for( int j=0; j<graph[i].size(); j++)
    {
        if( !visited[ getIndexOfVertex(vertices,numberOfVertices,graph[i][j]) ] )
            dfs( getIndexOfVertex(vertices,numberOfVertices,graph[i][j]), graph, visited, numberOfVertices, vertices );
    }
}
*/

void Dijkstra( int distanceFromStart[], string vertices[], vector< vector<string> > &graph, vector< vector<int> > &weights, int numberOfVertices )
{
    bool visited[ numberOfVertices ];
    for( int i=0; i<numberOfVertices; i++)
        visited[i] = false;

    string vertex = extractMinVertex(visited, vertices, distanceFromStart, numberOfVertices);

    while( vertex.length() != 0  )
    {
        visited[ getIndexOfVertex( vertices, numberOfVertices, vertex ) ] = true;
        relaxEdges( vertex, graph, weights, distanceFromStart, vertices, numberOfVertices);

        vertex = extractMinVertex(visited, vertices, distanceFromStart, numberOfVertices);
    }

}

void relaxEdges(string vertex, vector< vector<string> > &graph, vector< vector<int> > &weights, int distanceFromStart[], string vertices[], int numberOfVertices)
{
    int uIndex = getIndexOfVertex( vertices, numberOfVertices, vertex );

    for( int i=0; i<graph[uIndex].size(); i++ )
    {
        int vIndex = getIndexOfVertex(  vertices, numberOfVertices,graph[uIndex][i] );
        distanceFromStart[ vIndex ] = min( distanceFromStart[vIndex], distanceFromStart[uIndex] + weights[ uIndex ][ i ]  );
    }
}

string extractMinVertex(bool visited[], string vertices[], int distanceFromStart[],int numberOfVertices)
{
    string ret = "";
    for( int i=0; i<numberOfVertices; i++)
    {
        if( !visited[i] && ret.length() == 0)
            ret = vertices[i];
        else if( !visited[i] )
        {
            if( distanceFromStart[ getIndexOfVertex( vertices,numberOfVertices,ret) ] < distanceFromStart[ getIndexOfVertex( vertices,numberOfVertices,vertices[i] )] )
            {
                ret = vertices[i];
            }
        }
    }
    return ret;
}

int min( int a,int b)
{
    if( a<b )
        return a;
    return b;
}

int getIndexOfVertex(string vertices[], int numberOfVertices,string name)
{
    for( int i=0; i<numberOfVertices; i++)
    {
        if( vertices[i].compare(name) == 0 )
            return i;
    }
    return -1;
}


What I have tried:

I have tried to output 8 without success, i don't know how to finish the rest of the code
Posted
Updated 23-Nov-16 21:27pm

1 solution

Quote:
How to print the output 8
Technically, this is what you ask for:
C++
cout << 8;

But I am not sure it answer your problem.
My guess:
Yes, ultimately, your program don't "print the 8", but is it the problem ? I guess it is not.
What should do your program:
1) get the data
2) apply an algorithm to the data
3) print the answer to the algorithm, and Yes the expected answer is 8

As a programmer your job is also to track down the origin of bugs, and the debugger is an invaluable tool for this.
1) you need to make sure the entered data is correctly stored in variables, the debugger will help you to make sure of this.
2) you need to make sure your algorithm is doing what it should, here again use the debugger.
3) you need to make sure the result of the algorithm is printed correctly, here again use the debugger.

If the problem is in the algorithm, you know how the algorithm have to process the data, with the debugger, you will be able to the step by step what your program is really doing, by comparing with what it should do, the bug is when it is not a match.
don't forget to inspect variables on every step.
-----
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900