·
#include __FILE__

你给你的G.numArc赋值了0或INF,我想应该是G.arc,你没赋值就遍历数组里面就是垃圾值

然后我还改进了一下你的代码

#include<iostream>

const int MAX_VEX = 20;
const int ARC_INFINITY = 100;

class MGraph{
public:
    int numVertex, numArc;
    int arc[MAX_VEX][MAX_VEX];
    char vertex;
    MGraph(int numVertex, int numArc, char vertex='?') 
        :numVertex(numVertex), numArc(numArc), vertex(vertex) {
        for (int i = 0; i < numVertex; i++)
            for (int j = 0; j < numVertex; j++)
                arc[i][j] = i == j ? 0 : ARC_INFINITY;
    }
};

void Init_Mgraph(MGraph& G)
{
    std::cout << "numVertex: " << G.numVertex << "\tnumArc: " << G.numArc << std::endl;
    std::cout << "please input arc like: 0 1 13 \n";
    for (int i = 0; i < G.numArc; i++){
        int m, n, weight;
        std::cout << "input NO." << i + 1 << " arc\n";
        std::cin >> m >> n >> weight;
        G.arc[m][n] = G.arc[n][m] = weight;
    }

    for (int i = 0; i < G.numVertex; i++){
        for (int j = 0; j < G.numVertex; j++)
            std::cout << G.arc[i][j] << "\t\t";
        std::cout << std::endl;
    }
}


int main()
{
    MGraph G1(5, 4);
    Init_Mgraph(G1);
    return 0;
}
Replies
1

哥,你是我哥,谢谢