带有两个权重的有向加权图作为邻接矩阵C ++

时间:2015-12-04 05:58:18

标签: c++ matrix graph

这可能吗?

我使用邻接矩阵制作有向加权图。使用矩阵可以有两个权重吗?我可以使用邻接列表可视化如何执行此操作,但这需要重写我的所有代码。

这是我这个项目的全部代码。是的,这是为了学校。不,我不是要求你为我做这项工作。我似乎无法将头围绕在两个砝码上。

所以基本上,我在这里尝试做的是给程序两个权重。我将管理如何进行搜索和实际成本计算,我只是无法弄清楚我应该如何存储这样的两个权重。这两个权重是火车离开的时间,也是火车到达时的时间。之后,我将计算成本/路径/等。基于这两个权重。

我可以拥有一个有两个权重的矩阵吗?我怎么做?我不认为3D矩阵是本课的内容,虽然我认为这样可行(我的想法是有一个3D矩阵,其中z只有2深,所以我可以存储另一个整数"后面& #34;第一个)。但是,如果可能的话,我宁可不要。

正如你所看到的,我误认为任务的目的,并认为只是找到两次之间的差异然后存储,因为权重将起作用。但我需要知道节点x的离开与节点y的到达之间的区别。所以我认为我需要将它们传递到矩阵中然后计算我在做djikstra时获得路径成本。

有什么想法吗?

#include <iostream>
#include <iomanip>
#include <fstream>

#define MAX_SIZE 100
#define INFINITY 999999999
using namespace std;

class graph_matrix
{
    public:
        graph_matrix();
        graph_matrix(int, int);
        void setEdge(int, int, int);
        void djikstra(int, int);

    private:
        int nodeCount;
        int edgeCount;
        int **matrix;
};

void graph_matrix::djikstra(int source, int destination)
{
    int distances[nodeCount];
    int minNode, minDistance;
    bool vertexSet[nodeCount];

    for(int i=0; i<nodeCount; i++)
    {
        distances[i] = INFINITY;
        vertexSet[i] = false;
    }

    distances[source] = 0;

    for(int i = 0; i<nodeCount-1; i++)
    {
        minNode = INFINITY;
        minDistance = INFINITY;

        for(int vert = 0; vert<nodeCount; vert++)
        {
            if(vertexSet[vert] == false && distances[vert] <= minDistance)
            {
                minDistance = distances[vert];
                minNode = vert;
            }
        }

        vertexSet[minNode] = true;

        for(int vert = 0; vert<nodeCount; vert++)
        {
            if(vertexSet[vert] == false && matrix[minNode][vert])
            {
                if(distances[minNode] != INFINITY && distances[minNode] + matrix[minNode][vert] < distances[vert])
                {
                    distances[vert] = distances[minNode] + matrix[minNode][vert];
                }
            }
        }
    }

    if(distances[destination] < 60)
    {
        cout << distances[destination] << " minutes." << endl;  
    }

    else if(distances[destination] > 60)
    {
        cout << (distances[destination] - distances[destination]%60)/60 << " hours and " << distances[destination]%60 << " minutes." << endl;
    }
}

graph_matrix::graph_matrix()
{
    nodeCount = 0;
    edgeCount = 0;
    matrix = NULL;
}

graph_matrix::graph_matrix(int nodes, int edges)
{
    nodeCount = nodes + 1;
    edgeCount = edges;

    matrix = new int *[nodeCount];

    for (int i = 0; i <= nodeCount; i++)
        matrix[i] = new int[nodeCount];

    for (int i = 0; i < nodeCount; i++)
    {
        for (int x = 0; x < nodeCount; x++)
            matrix[i][x] = 0;
    }
}

void graph_matrix::setEdge(int source, int destination, int weight)
{
    matrix[source][destination] = weight;
}

int main()
{
    ifstream file;

    int statNumber, departStat, arriveStat, departTime, arriveTime, travelTime;
    string statName[MAX_SIZE];

    graph_matrix *gm = NULL;

    file.open("stations.dat");

    int stations = 0; //nodes
    int tracks = 0; //edges

    while(file >> statNumber)
    {
        file >> statName[statNumber];
        stations++;
    }

    file.close();

    file.open("trains.dat");


    while(file >> departStat)
    {
        file >> departStat;//I tried just using 'file;' here, but it didn't work. I didn't see any harm in passing it to a variable, though.
        file >> departStat;
        file >> departStat;
        tracks++;
        //I'm sure this is the least elegant way of doing this, but this is what my brain came up with
    }

    file.close();

    file.open("trains.dat");

    gm = new graph_matrix(stations, tracks);

    while(file >> departStat)
    {
        file >> arriveStat;
        file >> departTime;
        file >> arriveTime;
        travelTime = arriveTime - departTime; //At first, I thought this is what I needed.

        gm->setEdge(departStat, arriveStat, travelTime);
    }

    file.close();

    int menuin, numberin, departin, arrivein;
    string stationin;
    bool done = false;

    cout << "---------------------------------------------" << endl;
    cout << "        TRAIN MATE 3000            " << endl;
    cout << "---------------------------------------------" << endl << endl << endl;

    while(!done)
    {
        cout << "Options" << endl;
        cout << "(1) - Look up station by number" << endl;
        cout << "(2) - Look up station by name" << endl;
        cout << "(3) - Find nonstop train" << endl;
        cout << "(4) - Is service available" << endl;
        cout << "(5) - Shortest ride time" << endl;
        cout << "(6) - Quit" << endl << endl;

        cout << "?: ";

        cin >> menuin;

        cout << endl;

        switch (menuin)
        {
            case 1:
            {
                cout << ">Enter station number: ";
                cin >> numberin;
                cout << endl << ">>Station " << numberin << ": " << statName[numberin] << endl << endl;
                break;
            }

            case 2:
            {
                cout << ">Enter station name: ";
                cin >> stationin;
                for(int i=0; i<=stations; i++)
                {
                    if(stationin.compare(statName[i]) == 0)
                    {
                        cout << endl << ">>Station " << i << ": " << statName[i] << endl << endl;
                    }
                }
                break;
            }

            case 3:
            {
                cout << ">Enter departure station number: ";
                cin >> departin;
                cout << ">Enter arrival station number: ";
                cin >> arrivein;
                //do something to find if there's an edge
                break;
            }

            case 4:
            {
                cout << ">Enter departure station number: ";
                cin >> departin;
                cout << ">Enter arrival station number: ";
                cin >> arrivein;
                //do something to find if there's a path
                break;
            }

            case 5:
            {
                cout << ">Enter departure station number: ";
                cin >> departin;
                cout << ">Enter arrival station number: ";
                cin >> arrivein;
                cout << endl << ">>To go from " << statName[departin] << " to "<< statName[arrivein] << ", you will need to ride the train for ";
                gm->djikstra(departin, arrivein);
                cout << endl;
                break;
            }

            case 6:
            {
                done = true;
                break;
            }
        }
    }
}

抱歉,我的主要很长。不要担心任何这些。我只是询问矩阵本身,但我想我会给出整个背景。

0 个答案:

没有答案