所以,我需要做一个函数来返回一个图的色数。该图是通过一个形容词矩阵给出的,该函数使用一个文件名找到该矩阵。我有一个理论上应该可以工作的函数,编译器对它没有任何问题,但当我运行它时,它只是打印出一个空行并结束程序。
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int Find_Chromatic_Number (vector <vector <int>> matg, int matc[], int n) {
if (n == 0) {
return 0;
}
int result, i, j;
result = 0;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
if (matg[i][j] == 1) {
if (matc[i] == matc[j]) {
matc[j]++;
}
}
}
}
for (i = 0; i < n; i++) {
if (result < matc[i]) {
result = matc[i];
}
}
return result;
}
int main() {
string file;
int n, i, j, m;
cout << "unesite ime datoteke: " << endl;
cin >> file;
ifstream reader;
reader.open(file.c_str());
reader >> n;
vector<vector<int>> matg(n, vector<int>(0));
int matc[n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
reader >> matg[i][j];
}
matc[i] = 1;
}
int result = Find_Chromatic_Number(matg, matc, n);
cout << result << endl;
return 0;
}该程序应该使用freader将文件转换为表示形容词矩阵(matg)的2D向量。我还制作了一个数组(matc),它表示每个顶点的值,不同的数字对应不同的颜色。函数应该遍历向量,每次在两个顶点之间有一条边时,它应该检查它们在matc中的颜色值是否相同。如果是,则将第二个vale (j)加1。在函数传递完向量之后,matc数组应该包含n个不同的数字,其中最高的数字是我要查找的色数。我希望我已经对我试图实现的目标进行了足够的解释,如果不是只是询问,我将补充任何进一步的解释。
发布于 2020-01-13 02:18:36
试着让它变成那样。不要为你的矢量vector<vector<int> > matg;选择大小,而不是使用reader >> matg[i][j];:
int tmp;
reader >> tmp;
matg[i].push_back(tmp);https://stackoverflow.com/questions/59706641
复制相似问题