题目来源: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. 用AS3清空容器下所有子显示对象

    容器中的子显示对象分为两类: 处于显示列表中的子显示对象.被numChildren所记录的. 由容器graphics对象绘制出来的矢量图.这个矢量图不属于Shape类型,不在容器的显示列表中,不被nu ...

  2. Android Matrix(坐标矩阵)

    Android Matrix 2016-02-26 14:38:10 介绍 中文名:坐标矩阵 高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 在Android里面,Matr ...

  3. svn修改log信息

    在linux下安装了SVN服务器来做版本控制. 有天提交文件忘记了填写SVN提交日志,于是在项目中使用右键,show log,找到我提交的无日志的那条记录,点击右健,选择了“Edit log mess ...

  4. 基于visual Studio2013解决C语言竞赛题之0415特殊对数

       题目 解决代码及点评 这道题也是锻炼for循环,在for循环中遍历所有可能的数,然后再判断该数是不是有这样的性质 /********************************* ...

  5. OnPaint()函数的作用原理

    WM_PAINT是窗口每次重绘都会产生的一个消息. OnPaint是对这个消息的反应函数 mfc 的 CWnd::OnPaint 没做什么,只是丢给系统处理. 一 : 先执行OnEraseBkgnd, ...

  6. libvirt(virsh命令总结)

    virsh回车进入交互式界面: version pwd hostname 显示本节点主机名 nodeinfo  显示节点信息 list --all 显示所有云主机 7种状态: running  运行中 ...

  7. C Primer Plus 读书笔记之C基础回顾

    目标代码文件.可执行文件和库 C编程的基本策略是使用程序将源代码文件转换为可执行文件,此文件包含可以运行的机器语言代码.C分两步完成这一工作:编译和链接.编译器将源代码转换为中间代码,链接器将此中间代 ...

  8. selenium中用js定位html上没有id,没有name的元素

    所测试的页面包含display:none的元素,所以考虑用js,在使用js的过程中,js要获得页面的对象,没有id,没有name ...... <a class="employer&q ...

  9. SVN的trunk、branch、tag(二)

    转——简单的对比 SVN的工作机制在某种程度上就像一颗正在生长的树: 一颗有树干和许多分支的树 分支从树干生长出来,并且细的分支从相对较粗的树干中长出 一棵树可以只有树干没有分支(但是这种情况不会持续 ...

  10. Jquery学习笔记:通过层次关系获取jquery对象

    前面一篇文章,我们介绍了如何通过web标签的id , css样式值来获取jquery对象. 但这只是基本方法,不能满足所有场景的需求. 本文介绍通过dom元素之间的层次关系获取元素.具体是将各种标识符 ...