题目来源: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. 一入python深似海--浅拷贝与深拷贝

    python中有一个模块copy,deepcopy函数用于深拷贝,copy函数用于浅拷贝. 要理解浅拷贝,必须先弄清楚python中的引用. 引用 Python中一切都是对象,变量中存放的是对象的引用 ...

  2. C程序设计语言之一

    %d 按照十进制整形数打印: %o 按照八进制整形数打印: %x 按照十六进制整形数打印: %c 表示字符 %s 表示字符串 %% 表示%本身打印: %ld long型输出 ”幻数“: #define ...

  3. ViewPager+Fragment实现支持左右滑动的Tab

    主要思想:顶部标题栏top.xml,中间ViewPager(4个Fragment),底部导航 top.xml和bottom.xml在我之前的两个随笔里有,此处不再赘述. activity_main.x ...

  4. 断开/删除 SVN 链接(.svn)的几种方法

    上传到正式的服务器时需要去掉这些不必要的文件,找到了几种方法: 1.windows下: xcopy project_dir project_dir_1 /s /i (从project_dir 复制文件 ...

  5. jQuery prop 全选和全不全

    $('#ckAll').click(function() { var value = $(this).is(':checked') ? true : false; $("input[name ...

  6. [置顶] 使用红孩儿工具箱完成基于Cocos2d-x的简单游戏动画界面

    [Cocos2d-x相关教程来源于红孩儿的游戏编程之路CSDN博客地址:http://blog.csdn.net/honghaier 红孩儿Cocos2d-X学习园地QQ3群:205100149,47 ...

  7. C# 课堂总结1-二进制转换

    一.目的:便于计算机表示,稳定性好,符合逻辑运算,真为1,假为0. 二.各进制表示方法: 2进制:0,1 8进制:0-7 16进制:0-9,A,B,C,D,E,F 二.转换方法: 1.各进制转换为10 ...

  8. 再写KMP算法

    #include<iostream> #include<string> using namespace std; void getNext(char const*T,int l ...

  9. javascript:设置URL参数的方法,适合多条件查询

    适用场景:多条件查询情况,如下图所示: 通过设置URL参数,再结合数据源控件设置的RUL参数,就能进行简单的多条件查询了. javascript函数: <mce:script type=&quo ...

  10. JVM调优总结(九)-新一代的垃圾回收算法

    垃圾回收的瓶颈 传统分代垃圾回收方式,已经在一定程度上把垃圾回收给应用带来的负担降到了最小,把应用的吞吐量推到了一个极限.但是他无法解决的一个问题,就是Full GC所带来的应用暂停.在一些对实时性要 ...