题目来源: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. mvc请求过程总结

    前言 最近在思考一个问题,我的学习方法一般主要是看博客来学习新东西,但是光看,基本也没总结过,所以经常会出现这样的问题,某个知识点我知道,但是就是不能很好的表达出来,很简单的东西往往都不知道如何简短精 ...

  2. java实现随机验证码的图片

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc010006qo.html 1.一共需要2个常用java文件(RandomCode.java和RandomCod ...

  3. BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )

    题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...

  4. eclipse the user operation is waiting for building workspace" to complete

    "the user operation is waiting for building workspace" to complete", 解决办法: 1.选择菜单栏的“P ...

  5. Centos6.5搭建bugzilla

    一.安装httpd. mod_ssl. mysql-server . mysql .php-mysql . gcc . perl* . mod-perl-devel [root@localhost ~ ...

  6. 设计模式(四)原型模式Prototype(创建型)

      设计模式(四)原型模式Prototype(创建型) 1.   概述 我们都知道,创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象 ...

  7. Android 改变窗口标题栏的布局

    Android改变窗口标题栏的布局  第一种方式 --在XML文件里面引入配置文件作为标题. 第二种方式  --动态的代码加入进去. 第三种方式(网上的): 一. 重点 一般应用的Title都是建立应 ...

  8. [产值分析]生产部KPI考核之产值分析

    接到新任务:设计统计电子和磁电公司生产部产值分析报表. 眼下状况: 1.电子公司:取最新单位价格*入库数量 2.磁电公司:取最低价格*入库数量(实际取价的时候又没有取到最低价) 假设计算出来的结果和財 ...

  9. BZOJ 3375: [Usaco2004 Mar]Paranoid Cows 发疯的奶牛( set )

    果然写得短就跑得慢... 直接用set就行了(你要写棵平衡树也可以).没有包含的话, 假如L(i) <= L(j), 那么R[i] <= R[j]. 所以从小到大扫, 每次查找左端点小于当 ...

  10. 判断程序是否在VMWare内运行

    现在有许多用户都喜欢用虚拟机来测试他们的软件,以避免对真实机器环境造成损害.但是在虚拟机中,有些功能是受限,甚至不可能完成的,因此,需要在程序中判断虚拟机的环境,如果程序在虚拟机内运行,则就要把虚拟机 ...