c++ - 向量下标超出范围 MVS2013
问题描述
这是我用来查找图的 MST 的一些代码。我使用 scanf 在结构中输入边并将它们存储在具有边对象的向量中。我不明白为什么我会收到“向量下标超出范围错误”,因为我初始化了向量大小。
struct GraphEdge
{
int x, y, weight;
void readinput()
{
scanf("%d%d%d", &x, &y, &weight);
}
bool operator<(const GraphEdge &e) const
{
return weight < e.weight;
}
};
int main()
{
while (1)
{
int n, m;
int total_weight = 0;
scanf("%d%d", &n, &m);
if (!n && !m)
break;
int res = 0;
vector<GraphEdge> edge(m);
for (int i = 0; i < edge.size(); i++)
{
edge[i].readinput();
cout << edge[i].x << " " << edge[i].y << endl;
res += edge[i].weight;
//cout << i << endl;
}
sort(edge.begin(), edge.end());
union_find uf;
uf.init(n);
int cnt = 0, mst = 0;
for (int i = 0; cnt < n - 1 && i < m; i++)
{
if (uf.dstunion(edge[i].x, edge[i].y))
{
res -= edge[i].weight;
cnt++;
mst += edge[i].weight;
}
}
printf("%d\n", res);
}
return 0;
}
解决方案
推荐阅读
- ruby-on-rails - 仅在满足特定条件时运行一组验证的最佳实践?
- php - 如何通过添加到购物车查询字符串将多个可变产品添加到购物车
- python - 在不更改原始数据框的情况下向数据框的副本添加一列
- reactjs - 在 react-datepicker 中使用带有日期范围的 React-Redux 表单字段
- excel - VBA试图覆盖匹配记录并将不匹配的记录从ws1移动到ws2
- python - 使用 tensorflow、顺序预测时需要更高的准确性
- ios - XCode 自动完成功能不适用于扩展
- vba - 带有许多工作表和宏的慢速 Excel 文件
- mysql - 从 MySql 中的模式获取表行数
- android - 如何为录音机添加暂停选项