SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
题目来源: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最短路径算法的更多相关文章
- SRM 583 Div II Level One:SwappingDigits
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=2866&rd=5853 主要是要对字符串的操作要熟悉,熟 ...
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...
- SRM 582 Div II Level One: SemiPerfectSquare
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...
- SRM 582 Div II Level Two SpaceWarDiv2
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意 ...
- SRM 219 Div II Level One: WaiterTipping,小心约分
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...
- SRM 212 Div II Level One: YahtzeeScore
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1692&rd=5858 比较简单. 代码如下: #inc ...
随机推荐
- ThinkPHP - 博客获取列表信息
得到数据: Array ( [0] => Array ( [id] => 5 [name] => PHP [pid] => 0 [sort] => 1 [blog] =& ...
- 【Maven】项目添加Maven类库依赖
1.右击项目-->Maven-->EnableDependencyManagement,按步骤完成操作. 2.右击项目-->Properties-->DeploymentAss ...
- HDU 3932 Groundhog Build Home 【基础模拟退火】
和刚才那道是一模一样 不过求的是最小的,只要稍微修改一下就可以了~ //#pragma comment(linker, "/STACK:16777216") //for c++ C ...
- VirtualBox虚拟机安装RedHat7.3编译Linux0.01内核
引子 由于需要编译linux0.01内核,而目前的linux版本太高需要降低gcc版本等等,需要做不少调整非常不方便. 所以,直接安装RedHat7.3,这样就好编译linux0.01的内核了. 但是 ...
- Flask web开发 处理Session
本文我们在上篇文章<Flask web开发 处理POST请求(登录案例)>的基础上,来讲述Flask对session的支持. 在上面案例上,我们需要修改和新增如下功能 1.登录成功后的 ...
- wifi密码破解方法总结(含破解软件下载链接)
眼下网上流行有非常多无线password的破解方法,总结起来最有用的还是这两种:第一种是Wirelessnetview+WinAirCrackPack软件组合,这个方法简单方便:另外一种就是大家熟悉的 ...
- IOS中的数据存储 简单总结
1. NSKeyedArchiver(加密形式) 2. plist 3. NSUserDefaults 4. writeToFile 5. SQLite3 ==== N ...
- Qt状态机框架
The State Machine Framework 状态机框架提供了用于创建和执行状态图的类.概念和符号是基于Harel的Statecharts: A visual formalism for c ...
- Windows Phone 8初学者开发—第21部分:永久保存Wav音频文件
原文 Windows Phone 8初学者开发—第21部分:永久保存Wav音频文件 第21部分:永久保存Wav音频文件 原文地址:http://channel9.msdn.com/Series/Win ...
- [思路]为什么要做一个Web服务器
对于.net开发者而言,提到Web服务器最容易想到的就是IIS了. IIS功能强大,配置繁多,但不免对普通用户而言过于复杂,另外在云时代的今天,同时维护多个IIS或远程维护IIS还是有诸多不便的,有很 ...