整个代码段改为
// 输入int N, ML, MD;int AL[MAX_ML], BL[MAX_ML], DL[MAX_ML];int AD[MAX_MD], BD[MAX_MD], DD[MAX_MD];int d[MAX_N]; //最短距离bool updated; // 是否有更新void update(int& x, int y) {if (x > y) {x = y;updated = true;}}// 用Bellman-Ford算法计算dvoid bellmanford() {for (int k = 0; k <= N; k++) {updated = false;// 从i+1到i的权值为0for (int i = 0; i + 1 < N; i++) {if (d[i + 1] < INF) update(d[i], d[i + 1]);}// 从AL到BL的权值为DLfor (int i = 0; i < ML; i++) {if (d[AL[i] - 1] < INF) update(d[BL[i] - 1], d[AL[i] - 1] + DL[i]);}// 从BD到AD的权值为-DDfor (int i = 0; i < MD; i++) {if (d[BD[i] - 1] < INF) update(d[AD[i] - 1], d[BD[i] - 1] - DD[i]);}}}void solve() {// 检查是否存在负圈fill(d, d + N, 0);bellmanford();if (updated) {printf("-1\n");return;}fill(d, d + N, INF);d[0] = 0;bellmanford();int res = d[N - 1];if (res == INF) res = -2;printf("%d\n", res);}