如何在实例输入后打印输出8?

时间:2016-11-24 06:23:11

标签: c++ algorithm vector spanning-tree

我知道它背后的逻辑,但我不知道如何应用它来完成我的代码输出8。

获得8的方法是将最短路径添加到图中的每个顶点。最短路径是具有最少顶点数的路径。如果存在平局,则使用路径的权重作为平局判断器。

在输入如下所示的示例输入后,有人可以帮我完成输出8的代码吗?

输入:

4 6

女性保守派新保守派退伍军人

特朗普女士1

特朗普保守党1

特朗普新保守派5

女性Neocons 1

Neocons Veterans 5

保守派退伍军人1

输出:

8

找到下图中的图表 这是我的代码:

#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;
}

CLICK HERE

0 个答案:

没有答案