Michael_114 @ 2024-12-17 18:27:30
本人刚学SPFA -INF秒,求助
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 100;
struct Edge {
int to;
int cost;
Edge(int t, int c) : to(t), cost(c) {}
};
vector<Edge> graph[MAXN];
int dist[MAXN];
bool inQueue[MAXN];
void spfa(int start) {
memset(dist, 0x3f, sizeof(dist));
dist[start] = 0;
queue<int> q;
q.push(start);
inQueue[start] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
inQueue[u] = false;
for (const Edge& edge : graph[u]) {
int v = edge.to;
int cost = edge.cost;
if (dist[v] > dist[u] + cost) {
dist[v] = dist[u] + cost;
if (!inQueue[v]) {
q.push(v);
inQueue[v] = true;
}
}
}
}
}
int main() {
int a,b;
cin>>a>>b;
graph[0].push_back(Edge(a, a));
graph[0].push_back(Edge(b, b));
spfa(0);
cout <<dist[a] + dist[b] << endl;
return 0;
}
by wangyizhi @ 2024-12-17 18:31:49
@Michael_114
by Michael_114 @ 2024-12-17 18:33:09
@wangyizhi A……我下课调吧,太难了
by wangyizhi @ 2024-12-17 18:34:57
@Michael_114
改成
graph[0].push_back(Edge(1, a));
graph[0].push_back(Edge(2, b));
spfa(0);
cout <<dist[1] + dist[2] << endl;
应该就行了吧
by Michael_114 @ 2024-12-17 18:53:31
@wangyizhi谢谢!过了!