题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556

用Dijkstra实现,之前用Floyd算法写了一个,结果在2s内算不出结果来。

参考了别人算法,学到了set容器的一个用法,用set省去了查找Dijkstra算法中选择最短路径的那一步,set中的第一个元素就是最小值,用priority queue应该也可以。

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm> using namespace std; #define MAX_SIZE 40
#define INFINITY 100000 #define entry(x, y) make_pair(dd[x][y], make_pair(x, y)) int dijkstra(int x, int y);
vector <string> bcost; int dd[MAX_SIZE][MAX_SIZE];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int rows, cols;
class GameOnABoard
{
public:
int optimalChoice(vector <string> cost);
}; int GameOnABoard::optimalChoice(vector<string> cost)
{
int rows, cols, minL;
rows = cost.size();
cols = cost[0].size();
minL = INFINITY;
bcost = cost;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
minL = min(minL, dijkstra(i, j));
}
} return minL;
} int dijkstra(int x, int y)
{
int i, j, dir, maxC;
int cus_x, cus_y, next_x, next_y;
set < pair<int, pair<int, int>> > s;
rows = bcost.size();
cols = bcost[0].size(); for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
dd[i][j] = INFINITY;
}
}
dd[x][y] = bcost[x][y] - '0';
s.insert(entry(x, y)); while (!s.empty()) {
cus_x = s.begin()->second.first;
cus_y = s.begin()->second.second;
s.erase(s.begin()); for (dir = 0; dir < 4; dir++) {
next_x = cus_x + dx[dir];
next_y = cus_y + dy[dir];
if (next_x < 0 || next_x >= rows || next_y < 0 || next_y >= cols) {
continue;
} if (dd[next_x][next_y] <= dd[cus_x][cus_y] + bcost[next_x][next_y] - '0') {
continue;
}
s.erase( entry(next_x, next_y) );
dd[next_x][next_y] = dd[cus_x][cus_y] + bcost[next_x][next_y] - '0';
s.insert( entry(next_x, next_y) );
}
}
maxC = 0;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
maxC = max(maxC, dd[i][j]);
}
}
return maxC;
}

SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法的更多相关文章

  1. SRM 583 Div II Level One:SwappingDigits

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...

  2. SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...

  3. SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=2866&rd=5853 主要是要对字符串的操作要熟悉,熟 ...

  4. SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2

    题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...

  5. SRM 582 Div II Level One: SemiPerfectSquare

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...

  6. SRM 582 Div II Level Two SpaceWarDiv2

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...

  7. SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意  ...

  8. SRM 219 Div II Level One: WaiterTipping,小心约分

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...

  9. SRM 212 Div II Level One: YahtzeeScore

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1692&rd=5858 比较简单. 代码如下: #inc ...

随机推荐

  1. 如何成为CSDN博客专家

    先看一下官方给出的要求: 申请CSDN博客专家应具备的条件: 1.原创IT类文章总数超过20篇,并且最近一个月内发布了新的原创IT类文章. 2.博客文章总的浏览量超过5万次以上. 3.文章内容的质量很 ...

  2. BestCoder Round #50 (div.1) 1001 Distribution money (HDU OJ 5364)

    题目:Click here 题意:bestcoder上面有中文题目 #include <iostream> #include <cstdio> #include <cst ...

  3. 今天才知道mysql

    insert MySQL中的INSERT语句和标准的INSERT不太一样,在标准的SQL语句中,一次插入一条记录的INSERT语句只有一种形式.INSERT INTO tablename(列名…) V ...

  4. 生成pdf文件

  5. javascript入门视频第一天 小案例制作 零基础开始学习javascript

    JavaScript 是我们网页设计师必备的技能之一.我们主要用javascript来写的是网页特效.我们从零基础开始学习javascript入门. 但是,好的同学刚开始不知道怎么学习,接触js,因此 ...

  6. javaScript 网页特效 输出语句

    大家好,我是小强老师,今天主要讲解 三个最为常用的输出语句.  alert() 弹出警示框 window.alert(‘继续学习’);  完整的写法 效果如下: 因为alert 属于window 对象 ...

  7. 一步一步重写 CodeIgniter 框架 (8) —— 视图的嵌套输出与返回

    视图函数在控制器中通过 $this->load-view() 来调用,从而输出 html,有时候为了调试或附加处理的需要,我们需要打印出这些输出,而不是直接通过浏览器输出,这在 php 中是通过 ...

  8. ICE之C/S通信原理

    /* 在ICE文档中只需要声明module名称,接口名称,方法名称 */ #ifndef SIMPLE_ICE #define SIMPLE_ICE module Demo{ //module名称 i ...

  9. arm:jlink调试和直接烧写运行的不同 [mdk s3c2440]

    1.对全局变量的初始化. 2.还没发现的事例. /*************************************************/ 先上连接文件sct LR_ROM1 0x3000 ...

  10. SQL Server 基础 02 确保数据完整性

    本章总结目的:    为了巩固 约束.事务! 约 束 使用数据库约束就是保证数据库的完整性的方法,SQL Server 涉及的完整性有三个: 1.实体完整性 : (不能为空且重复,即唯一的,例如身份证 ...