Niko_Hu @ 2023-03-29 18:46:52
如题,SPFA算法迭代过程中不是会求出所有到达n点的最短路径吗? 那为什么不新增一个数组cnt维护走到当前点的路径上花费最大的城市 用ans来记录答案,当每次走到n点时候更新ans
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int M = 1e4 + 10;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int h[M];
int n, m;
long long b;
int e[N], w[N], ne[N], idx;
long long dist[N];//走到i点消耗的最小的血量
int cnt[N];//走到i点路径中最大花费的城市花了多少钱
int s[N];//每个城市的消费
bool st[N];
int ans = INF;
bool flag = true;
void insert(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
void spfa() {
memset(dist, 0x3f, sizeof dist);
queue<int> q;
dist[1] = 0;
st[1] = true;
q.push(1);
while (q.size()) {
int ver = q.front();
q.pop();
st[ver] = false;
for (int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[ver] + w[i]) {
dist[j] = dist[ver] + w[i];
cnt[j] = max(cnt[ver], s[j]);
if (j == n&&dist[ver]<=b) {
flag = false;
ans = min(cnt[j], ans);
}
if (!st[j]) {
q.push(j);
st[j] = true;
}
}
}
}
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m >> b;
for (int i = 1; i <= n; i++)scanf("%d", &s[i]);
while (m--) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
insert(a, b, c);
insert(b, a, c);
}
spfa();
if (flag)cout << "AFK";
else cout << ans;
}
这是代码,但只能得70分,求解为啥错了