#include<iostream>
#include<vector>
using std::vector;
using namespace std;
int x;
vector<vector<int>>member(x);//矩阵成员;
void set_point(int k)
{
int count=0;
int min=99;
vector<int>dist(x);//储存当前结点的全部路径;
for (int i = 0; i < x; i++)
dist[i] = member[k][i];
vector<int>solve(x);//当前的经历过的结点;
for (auto &ele : solve)
ele = -1;
solve[k] = 1;
for (int s = 1; s < x; s++)
{
for (int i = 0; i < x; i++)
{
if (dist[i] < dist[i + 1] && solve[i] == -1)
min = i;
}
solve[min] = 1;
count++;
for (int i = 0; i < x&&solve[i] == -1; i++)
for (int j = 0; j < x&&solve[j] == -1; j++)
if (dist[j] > member[k][i] + member[i][j])
dist[j] = member[k][i] + member[i][j];//出问题的for循环
}
}
void set_vertex()
{
cout << “请规定矩阵的大小” << endl;
cin >> x;
member.resize(x);
for (int i = 0; i < x;i++)
member[i].resize(x);
cout << “请输入各矩阵的值” << endl;
for (int i = 0; i<x; i++)
for (int j = 0; j < x;j++ )
cin >> member[i][j];
cout << “请输入想查找的第一个结点” << endl;
int k;
cin >> k;
set_point(k);
}
int main()
{
set_vertex();
}
#include<vector>
using std::vector;
using namespace std;
int x;
vector<vector<int>>member(x);//矩阵成员;
void set_point(int k)
{
int count=0;
int min=99;
vector<int>dist(x);//储存当前结点的全部路径;
for (int i = 0; i < x; i++)
dist[i] = member[k][i];
vector<int>solve(x);//当前的经历过的结点;
for (auto &ele : solve)
ele = -1;
solve[k] = 1;
for (int s = 1; s < x; s++)
{
for (int i = 0; i < x; i++)
{
if (dist[i] < dist[i + 1] && solve[i] == -1)
min = i;
}
solve[min] = 1;
count++;
for (int i = 0; i < x&&solve[i] == -1; i++)
for (int j = 0; j < x&&solve[j] == -1; j++)
if (dist[j] > member[k][i] + member[i][j])
dist[j] = member[k][i] + member[i][j];//出问题的for循环
}
}
void set_vertex()
{
cout << “请规定矩阵的大小” << endl;
cin >> x;
member.resize(x);
for (int i = 0; i < x;i++)
member[i].resize(x);
cout << “请输入各矩阵的值” << endl;
for (int i = 0; i<x; i++)
for (int j = 0; j < x;j++ )
cin >> member[i][j];
cout << “请输入想查找的第一个结点” << endl;
int k;
cin >> k;
set_point(k);
}
int main()
{
set_vertex();
}
解决方案
40
#include<iostream> #include<vector> using std::vector; using namespace std; int x; vector<vector<int>>member(x);//矩阵成员; void set_point(int k) { int count=0; int min=99; vector<int>dist(x);//储存当前结点的全部路径; dist.resize(x); for (int i = 0; i < x; i++) dist[i] = member[k][i]; vector<int>solve(x);//当前的经历过的结点; solve.resize(x); for (auto &ele : solve) ele = -1; solve[k] = 1; for (int s = 1; s < x; s++) { for (int i = 0; i < x-1; i++) { if (dist[i] < dist[i + 1] && solve[i] == -1) min = i; } solve[min] = 1; count++; for (int i = 0; i < x&&solve[i] == -1; i++) for (int j = 0; j < x&&solve[j] == -1; j++) if (dist[j] > member[k][i] + member[i][j]) dist[j] = member[k][i] + member[i][j];//出问题的for循环 } } void set_vertex() { cout << "请规定矩阵的大小" << endl; cin >> x; member.resize(x); for (int i = 0; i < x;i++) member[i].resize(x); cout << "请输入各矩阵的值" << endl; for (int i = 0; i<x; i++) for (int j = 0; j < x;j++ ) cin >> member[i][j]; cout << "请输入想查找的第一个结点" << endl; int k; cin >> k; set_point(k); } int main() { set_vertex(); }