有没有大佬来看一下到底错哪了
原题链接:21569.汽车加油行驶问题2024-08-25 10:29:16
发布于:天津
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <climits>
using namespace std;
const int INF = INT_MAX;
const int dx[] = {1, 0, -1, 0}; // Directions: Right, Down, Left, Up
const int dy[] = {0, 1, 0, -1};
int main() {
int N, K, A, B, C;
cin >> N >> K >> A >> B >> C;
vector<vector<int>> grid(N + 1, vector<int>(N + 1));
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
cin >> grid[i][j];
}
}
vector<vector<vector<int>>> dist(N + 1, vector<vector<int>>(N + 1, vector<int>(K + 1, INF)));
priority_queue<tuple<int, int, int, int>, vector<tuple<int, int, int, int>>, greater<>> pq;
dist[1][1][K] = 0;
pq.emplace(0, 1, 1, K);
while (!pq.empty()) {
int cost, x, y, fuel;
tie(cost, x, y, fuel) = pq.top();
pq.pop();
if (x == N && y == N) {
cout << cost << endl;
return 0;
}
if (cost > dist[x][y][fuel]) continue;
for (int d = 0; d < 4; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 1 || nx > N || ny < 1 || ny > N) continue;
int next_fuel = fuel - 1;
int next_cost = cost;
if (nx < x || ny < y) next_cost += B; // Movement cost
if (next_fuel < 0) {
if (grid[x][y] == 1) {
next_cost += C;
next_fuel = K;
} else {
continue;
}
}
if (grid[nx][ny] == 1 && next_fuel < K) {
next_cost += A;
next_fuel = K;
}
if (next_cost < dist[nx][ny][next_fuel]) {
dist[nx][ny][next_fuel] = next_cost;
pq.emplace(next_cost, nx, ny, next_fuel);
}
}
}
cout << -1 << endl; // Should never reach here for valid inputs
return 0;
}
全部评论 3
分层图最短路模板题,套个模版秒了
6天前 来自 湖南
0为啥要加英文注解
2024-12-13 来自 浙江
0因为是用GPT写的,作者忘删了(
2024-12-13 来自 广东
0额
2024-12-13 来自 浙江
0
代码错了<-废话
2024-08-27 来自 广东
0
有帮助,赞一个