C++ “这句话是什么意思?”;向量<;朗朗>;距离(n,标准::数值限制<;长>;::最大值())&引用;?
实际上,我必须找到从源顶点到所有其他顶点的最短路径。为此,我提供了下面给出的代码模板。我想实现“C++ “这句话是什么意思?”;向量<;朗朗>;距离(n,标准::数值限制<;长>;::最大值())&引用;?,c++,vector,graph,bellman-ford,C++,Vector,Graph,Bellman Ford,实际上,我必须找到从源顶点到所有其他顶点的最短路径。为此,我提供了下面给出的代码模板。我想实现“Bellman–Ford算法”” #包括 #包括 #包括 #包括 使用std::vector; 使用std::queue; 使用std::pair; 使用std::priority_队列; 无效最短路径(向量与调整、向量与成本、整数、向量与距离、向量与可达、向量与最短){ //在这里编写代码 } int main(){ int n,m,s; 标准:cin>>n>>m; 向量adj(n,向量()); 向
Bellman–Ford算法”
”
#包括
#包括
#包括
#包括
使用std::vector;
使用std::queue;
使用std::pair;
使用std::priority_队列;
无效最短路径(向量与调整、向量与成本、整数、向量与距离、向量与可达、向量与最短){
//在这里编写代码
}
int main(){
int n,m,s;
标准:cin>>n>>m;
向量adj(n,向量());
向量成本(n,向量());
for(int i=0;i>x>>y>>w;
形容词[x-1]。推回(y-1);
成本[x-1]。推回(w);
}
标准:cin>>s;
s--;
向量距离(n,std::numeric_limits::max());
向量可达(n,0);
向量最短(n,1);
最短路径(adj,成本,s,距离,可达,最短);
对于(int i=0;i向量
-变量的类型
距离
-变量名称
(n,std::numeric_limits::max());
-构造函数参数
std::numeric\u limits::max()
-std::numeric\u limits
是一个需要类型参数的类(此处为long
)它有静态函数,返回专门用于此类型的值。在这种情况下,long
的最大值您是否尝试过搜索例如std::numeric\u limits
?您找到了什么?至于distance
行,它刚刚定义了一个变量distance
,类型为std::vec>tor
,并将参数传递给合适的(在该引用中更准确地说是数字3)。@Someprogrammerdude实际上我得到了-“numeric_limits类模板提供了一种标准化的方法来查询算术类型的各种属性(例如,类型int的最大可能值是std::numeric_limits::max())”。但我无法理解。为什么我们不能使用“INT_MAX”来实现这一目的呢?因为INT_MAX
是INT
的最大值,而不是long
数值限制不仅仅是最小值和最大值。
#include <iostream>
#include <limits>
#include <vector>
#include <queue>
using std::vector;
using std::queue;
using std::pair;
using std::priority_queue;
void shortest_paths(vector<vector<int> > &adj, vector<vector<int> > &cost, int s, vector<long long> &distance, vector<int> &reachable, vector<int> &shortest) {
//write your code here
}
int main() {
int n, m, s;
std::cin >> n >> m;
vector<vector<int> > adj(n, vector<int>());
vector<vector<int> > cost(n, vector<int>());
for (int i = 0; i < m; i++) {
int x, y, w;
std::cin >> x >> y >> w;
adj[x - 1].push_back(y - 1);
cost[x - 1].push_back(w);
}
std::cin >> s;
s--;
vector<long long> distance(n, std::numeric_limits<long long>::max());
vector<int> reachable(n, 0);
vector<int> shortest(n, 1);
shortest_paths(adj, cost, s, distance, reachable, shortest);
for (int i = 0; i < n; i++) {
if (!reachable[i]) {
std::cout << "*\n";
} else if (!shortest[i]) {
std::cout << "-\n";
} else {
std::cout << distance[i] << "\n";
}
}
}