NSOJ 鬼泣
今天组队赛的一道最短路的题,给你一个矩阵,矩阵上有L,R,G,A,分别表示当你到达这个点的时候你要向左,向右,向前,向后走,如果要向别的方向走需要花费1点的魔力,正常情况下走需要花费1点的时间。问花费最小魔力的时候的最少时间是多少。 一个机智的处理方法是向别的方向走的时候的花费1*100000000,然后构图跑最短路出来的结果/100000000就是最小的魔力,%100000000就是最小的时间。
但是比赛的时候WA了好久,赛后RE了好久。终于发现一个严重的问题----队列开小了。
平时最短路都是STL里的队列,这次用了手写的队列,然后手写的队列又没有弄成循环的队列,所以就跪了。写下来警醒自己:
手写队列要小心!!!!!
手写队列要小心!!!!!
手写队列要小心!!!!!
#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <ctime>
using namespace std; #define maxn 120
#define ll long long
int n, m; int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 }; const ll hcost = 100000000; int cd(int k, char cmd){
if (cmd == 'G') return k;
else if (cmd == 'L') return (k + 3) % 4;
else if (cmd == 'R') return (k + 1) % 4;
else return (k + 2) % 4;
} char b[maxn][maxn]; struct Edge{
int v; ll w;
Edge(int vi, ll wi) :v(vi), w(wi){}
Edge(){}
};
vector<Edge> G[maxn*maxn * 8]; int getid(int i, int j, int k){
return 4 * (i*m + j) + k;
} bool in[maxn*maxn * 8];
ll d[maxn*maxn * 8];
bool vis[maxn*maxn * 8]; void spfa()
{
memset(in, 0, sizeof(in));
memset(d, 0x3f, sizeof(d));
memset(vis, 0, sizeof(vis));
in[0] = true; d[0] = 0;
queue<int> que;
que.push(0);
while (!que.empty()){
int u = que.front(); in[u] = false; que.pop();
for (int i = 0; i < G[u].size(); ++i){
int v = G[u][i].v;
ll w = G[u][i].w;
if (d[u] + w < d[v]){
d[v] = d[u] + w;
if (!in[v]) {
que.push(v);
in[v] = true;
}
}
}
}
} int main()
{
//freopen("C.in", "r", stdin);
//freopen("out.txt", "w", stdout);
//double t1 = clock();
while (cin >> n >> m){
for (int i = 0; i < n; ++i){
scanf("%s", b[i]);
}
for (int i = 0; i <= n*m * 6; ++i){
G[i].clear();
}
for (int i = 0; i < n; ++i){
for (int j = 0; j < m; ++j){
for (int k = 0; k < 4; ++k){
int id = getid(i, j, k);
char cmd = b[i][j];
int cord = cd(k, cmd);
int xx, yy;
for (int kk = 0; kk < 4; ++kk){
xx = i + dx[kk]; yy = j + dy[kk];
if (!(xx >= 0 && xx < n&&yy >= 0 && yy < m)) continue;
if (kk == cord) {
G[id].push_back(Edge(getid(xx, yy, kk), 1LL));
}
else{
G[id].push_back(Edge(getid(xx, yy, kk), hcost + 1));
}
}
}
}
}
spfa();
ll ans = 1e15;
for (int i = 0; i < 4; ++i){
int idd = getid(n - 1, m - 1, i);
ans = min(ans, d[idd]);
}
printf("%lld %lld\n", ans / hcost, ans%hcost);
}
//double t2 = clock();
//cout << t2 - t1 << endl;
return 0;
}
NSOJ 鬼泣的更多相关文章
- NSOJ A fairy tale of the two(最小费用最大流、SPFA版本、ZKW版本)
n,m<=20,给两个n×m布尔矩阵,每次操作可将第一个矩阵的2个相邻元素互换.输出最少操作次数使得两个矩阵完全一样. 比赛的时候想过按照二分图完美匹配的类似做法构图,不过想到边太多以及卡各种题 ...
- NSOJ 飞船汇合(经典)
一支分散的飞船舰队,需要汇合到主舰,但是这种飞船在太空中飞行的耗油与质量没有关系,只与发动机打开的时间有关系,为了节省油量,指挥官通知,汇合途中,多台飞船可以串成串飞行,这样只需启动一台发动机,由于安 ...
- NSOJ Minimum Transport Cost
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- NSOJ 一个人的旅行(图论)
虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿 ...
- NSOJ Constructing Roads(图论)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- NSOJ 畅通工程(并查集)
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...
- NSOJ 4621 posters (离散化+线段树)
posters 时间限制: 1000ms 内存限制: 128000KB 64位整型: Java 类名: 上一题 提交 运行结果 统计 讨论版 下一题 题目描述 The citizens of ...
- DEVIL MAY CRY V:《鬼泣5》
“又是一个带孝子的故事”
- VR原理讲解及开发入门
本文是作者obuil根据多年心得专门为想要入门的VR开发者所写,由52VR网站提供支持. 1. VR沉浸感和交互作用产生的原理: 在之前,我们观看一个虚拟的创造内容是通过平面显示器的,52VR ...
随机推荐
- JavaWeb之Servlet:Cookie 和 Session
会话 现实生活中我们会用手机跟对方对话,拿起手机,拨号,然后对面接听,跟着互相通话,最后会话结束. 这个过程也可以用我们的B/S模式来描述: 打开浏览器—>输入地址->发出请求->服 ...
- vim代码补全-spf13,YouCompleteMe
vim代码补全 现在的图形界面的IDE(Integrated Development Environment)一般具有语法高亮,语法检查,自动补全功能,大大提高了编程的效率. vim作为文本编辑器其强 ...
- C语言--通用类型栈
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h&g ...
- Python pexpect出现错误‘module have no attribute "spawn" 解决办法
今天我遇到了这个错误,现在将错误总结如下: 1.首先查询一下自己的操作系统,pexpect中的spawn()和run()仅仅运行在POSIX系统上,在WINDOWS下是没有这两个东西的,在官网http ...
- echo换行的蛋疼事
openstack@openstack:~$ echo "abc" | shasum03cfd743661f07975fa2f1220c5194cbaff48451 -而使用Ja ...
- iOS-打包成ipa
第一步:模拟器选择栏,选择"Generic iOS Device ".早期版本需要断开手机连接,才可以找到. 第二步:选择"Product"菜单下的" ...
- simplexml_load_string 解析gb2312编码的xml
<?php header('Content-type:text/html;charset=UTF-8'); $url = 'http://www.xxx.com/text.xml'; $cont ...
- iOS8中如何将状态栏的字体颜色改为白色
网上的一些方法在我这行不通, 比如: UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent ...
- 通过store为toolbar添加按钮
目的是实现导航条toolbar可以动态加载按钮. ExtJS的版本是4.0. 实现方案有两个.方案一:添加render事件监听,在监听事件处理函数中使用Ext.Ajax获取按钮信息,实现动态添加按钮. ...
- [转]What’s Behind Ericsson’s OpenWebRTC Project?
[转]What’s Behind Ericsson’s OpenWebRTC Project? http://www.tuicool.com/articles/z6rAVrJ Ericsson’s O ...