結果
| 問題 | No.34 砂漠の行商人 |
| コンテスト | |
| ユーザー |
data9824
|
| 提出日時 | 2015-06-15 20:18:32 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 95 ms / 5,000 ms |
| コード長 | 1,730 bytes |
| コンパイル時間 | 975 ms |
| コンパイル使用メモリ | 83,784 KB |
| 実行使用メモリ | 6,528 KB |
| 最終ジャッジ日時 | 2024-06-28 10:17:43 |
| 合計ジャッジ時間 | 2,329 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <limits>
using namespace std;
int n, v;
struct Node {
int node;
int depth;
int distance;
Node(int node, int depth, int distance) :
node(node), depth(depth), distance(distance) {}
};
int shortest(const vector<map<int, int> >& costs, int nodeCount, int startNode, int endNode) {
queue<Node> q;
vector<int> distances(nodeCount, numeric_limits<int>::max());
q.push(Node(startNode, 0, 0));
while (!q.empty()) {
Node node = q.front();
q.pop();
if (distances[node.node] > node.distance) {
distances[node.node] = node.distance;
if (node.node == endNode && node.distance < v) {
return node.depth;
}
for (map<int, int>::const_iterator it = costs[node.node].begin();
it != costs[node.node].end();
++it) {
q.push(Node(it->first, node.depth + 1, node.distance + it->second));
}
}
}
return -1;
}
int index(int x, int y) {
return (x - 1) + (y - 1) * n;
}
int main() {
int sx, sy, gx, gy;
cin >> n >> v >> sx >> sy >> gx >> gy;
vector<vector<int> > l(n + 1, vector<int>(n + 1));
for (int y = 1; y <= n; ++y) {
for (int x = 1; x <= n; ++x) {
cin >> l[x][y];
}
}
int nodeCount = n * n;
vector<map<int, int> > edges(nodeCount);
for (int y = 1; y <= n; ++y) {
for (int x = 1; x <= n; ++x) {
if (x > 1) {
edges[index(x - 1, y)][index(x, y)] = l[x][y];
}
if (x < n) {
edges[index(x + 1, y)][index(x, y)] = l[x][y];
}
if (y > 1) {
edges[index(x, y - 1)][index(x, y)] = l[x][y];
}
if (y < n) {
edges[index(x, y + 1)][index(x, y)] = l[x][y];
}
}
}
int result = shortest(edges, nodeCount, index(sx, sy), index(gx, gy));
cout << result << endl;
return 0;
}
data9824