Compile Error
foo.cc: In function 'int dijkstra(int, int, int, int, std::vector<std::vector<char> >&)':
foo.cc:19:79: error: wrong number of template arguments (0, should be 1)
19 | priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;
| ^
In file included from /usr/include/c++/13/string:49,
from /usr/include/c++/13/bits/locale_classes.h:40,
from /usr/include/c++/13/bits/ios_base.h:41,
from /usr/include/c++/13/ios:44,
from /usr/include/c++/13/ostream:40,
from /usr/include/c++/13/iostream:41,
from foo.cc:1:
/usr/include/c++/13/bits/stl_function.h:393:12: note: provided for 'template<class _Tp> struct std::greater'
393 | struct greater : public binary_function<_Tp, _Tp, bool>
| ^~~~~~~
foo.cc:19:80: error: template argument 3 is invalid
19 | priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;
| ^~
foo.cc:21:8: error: request for member 'push' in 'pq', which is of non-class type 'int'
21 | pq.push(make_tuple(0, 0, 0));
| ^~~~
foo.cc:29:16: error: request for member 'empty' in 'pq', which is of non-class type 'int'
29 | while (!pq.empty()) {
| ^~~~~
foo.cc:30:39: error: request for member 'top' in 'pq', which is of non-class type 'int'
30 | auto [currentCost, r, c] = pq.top();
| ^~~
foo.cc:31:12: error: request for member 'pop' in 'pq', which is of non-class type 'int'
31 | pq.pop();
| ^~~
foo.cc:43:20: error: request for member 'push' in 'pq', which is of non-class type 'int'
43 | pq.push(make_tuple(cost[new_row][new_col], new_row, new_col));
| ^~~~
foo.cc:53:20: error: request for member 'push' in 'pq', which is of non-class type 'int'
53 | pq.push(make_tuple(cost[new_row][new_col], new_row, new_col));
| ^~~~
Code
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <tuple>
using namespace std;
struct Point {
int row, col;
};
bool isValid(int row, int col, int N, int M, vector<vector<char>> &grid, vector<vector<int>> &cost) {
return row >= 0 && row < N && col >= 0 && col < M && grid[row][col] != '#' && cost[row][col] == INT_MAX;
}
int dijkstra(int N, int M, int x, int y, vector<vector<char>> &grid) {
vector<vector<int>> cost(N, vector<int>(M, INT_MAX));
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;
pq.push(make_tuple(0, 0, 0));
cost[0][0] = 0;
// Directions for straight and L-shaped pipes
vector<int> dr = {1, 0};
vector<int> dc = {0, 1};
vector<pair<int, int>> lshape = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
while (!pq.empty()) {
auto [currentCost, r, c] = pq.top();
pq.pop();
if (r == N - 1 && c == M - 1) {
return currentCost;
}
// Move straight
for (int i = 0; i < 2; ++i) {
int new_row = r + dr[i];
int new_col = c + dc[i];
if (isValid(new_row, new_col, N, M, grid, cost) && currentCost + x < cost[new_row][new_col]) {
cost[new_row][new_col] = currentCost + x;
pq.push(make_tuple(cost[new_row][new_col], new_row, new_col));
}
}
// Move L-shaped (avoiding invalid paths)
for (auto [drL, dcL] : lshape) {
int new_row = r + drL;
int new_col = c + dcL;
if (isValid(new_row, new_col, N, M, grid, cost) && currentCost + y < cost[new_row][new_col]) {
cost[new_row][new_col] = currentCost + y;
pq.push(make_tuple(cost[new_row][new_col], new_row, new_col));
}
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int N, M, x, y;
cin >> N >> M >> x >> y;
vector<vector<char>> grid(N, vector<char>(M));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
cin >> grid[i][j];
}
}
int result = dijkstra(N, M, x, y, grid);
cout << result << "\n";
}
return 0;
}
Information
- Submit By
- Type
- Submission
- Problem
- P1156 G. Low cost water management
- Contest
- Happy New Year 2025
- Language
- C++11 (G++ 13.2.0)
- Submit At
- 2025-01-02 16:42:55
- Judged At
- 2025-01-02 16:42:55
- Judged By
- Score
- 0
- Total Time
- 0ms
- Peak Memory
- 0 Bytes