1321. Robot
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Karell Incorporated has designed a new exploration robot that has the ability to explore new terrains, this new robot can move in all kinds of terrain, it only needs more fuel to move in rough terrains, and less fuel in plain terrains. The only problem this robot has is that it can only move orthogonally, the robot can only move to the grids that are at the North, East, South or West of its position.
The Karell`s robot can communicate to a satellite dish to have a picture of the terrain that is going to explore, so it can select the best route to the ending point, The robot always choose the path that needs the minimum fuel to complete its exploration, however the scientist that are experimenting with the robot, need a program that computes the path that would need the minimum amount of fuel. The maximum amount of fuel that the robot can handle is 9999 units
The Terrain that the robot receives from the satellite is divided into a grid, where each cell of the grid is assigned to the amount of fuel the robot would need to pass thought that cell. The robot also receives the starting and ending coordinates of the exploration area.
![\epsfbox{p3481.eps}](http://www.soj.me/UserFiles/c57/1004_1.gif)
Input
The first line of the input file is the number of tests that must be examined.
The first line of the test is the number of rows and columns that the grid will contain. The rows and columns will be 0 < row100 , 0 <column
100
The next lines are the data of the terrain grid
The last line of the test has the starting and ending coordinates.
Output
One line, for each test will have the amount of fuel needed by the robot
Sample Input
3 5 5 1 1 5 3 2 4 1 4 2 6 3 1 1 3 3 5 2 3 1 2 2 1 1 1 1 1 1 5 5 5 4 2 2 15 1 5 1 15 1 5 3 10 1 5 2 1 1 8 13 2 15 1 1 1 4 10 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10
Sample Output
10 15 19
Dijkstra
怎么优化都是0.01s,当然我才刚刚开始学Dijkstra。。。
这是最开始的:
// Problem#: 1321
// Submission#: 2776602
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <vector>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAX 105
#define INF 99999999
int h, w, mapp[MAX][MAX], si, sj, ei, ej;
bool done[MAX * MAX];
struct edge {
int to;
int dis;
edge(int new_to, int new_dis): to(new_to), dis(new_dis){}
};
vector<edge> e[MAX * MAX];
void make_roads() {
int pos_i;
for (int i = 0; i < MAX * MAX; i++) {
e[i].clear();
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
pos_i = i * 100 + j;
if (i > 0) {
e[pos_i].push_back(edge(pos_i - 100, mapp[i - 1][j]));
}
if (j > 0) {
e[pos_i].push_back(edge(pos_i - 1, mapp[i][j - 1]));
}
if (i < h - 1) {
e[pos_i].push_back(edge(pos_i + 100, mapp[i + 1][j]));
}
if (j < w - 1) {
e[pos_i].push_back(edge(pos_i + 1, mapp[i][j + 1]));
}
}
}
si--;
sj--;
ei--;
ej--;
}
typedef pair<int, int> p;
int Dijkstra() {
memset(done, 0, sizeof(done));
int sp = si * 100 + sj;
int ep = ei * 100 + ej;
int d[MAX * MAX];
fill(d, d + MAX * MAX, INF);
d[sp] = 0;
priority_queue<p, vector<p>, greater<p> > q;
q.push(p(0, sp));
p top;
while (!q.empty()) {
top = q.top();
q.pop();
if (top.second == ep)
return d[ep];
if (done[top.second] || d[top.second] < top.first)
continue;
done[top.second] = 1;
for (int i = 0; i < (int)e[top.second].size(); i++) {
if (d[e[top.second][i].to] > d[top.second] + e[top.second][i].dis) {
d[e[top.second][i].to] = d[top.second] + e[top.second][i].dis;
q.push(p(d[e[top.second][i].to], e[top.second][i].to));
}
}
}
return d[ep];
}
int main() {
int case_num;
scanf("%d", &case_num);
while (case_num--) {
scanf("%d%d", &h, &w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d", &mapp[i][j]);
}
}
scanf("%d%d%d%d", &si, &sj, &ei, &ej);
make_roads();
printf("%d\n", Dijkstra() + mapp[si][sj]);
}
return 0;
}