Sicily 1321. Robot

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}
Path Example
From (1,1) to (5,5)
Fuel needed 10

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 < row$ \le$100 , 0 <column$ \le$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;
}                           


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值