经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。

徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?

请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。

Input输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);

第二行有徐总的所在地start,他的目的地end;

接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。

note:一组数据中地名数不会超过150个。

如果N==-1,表示输入结束。

Output如果徐总能到达目的地,输出最短的时间;否则,输出“-1”。

Sample Input

6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1

Sample Output

50

Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake
本题其实一点都不难,只要会最短路和一个STl中的map容器就可以了。。。
为啥要用map容器,我感觉用其他的肯定要麻烦,用map可以直接找到,不用很循环来找这个地区所对应的编号(编号??什么编号。。。你不用编号来代替最短路的边是从谁向谁那怎么办
。。。)
最后要注意的就是就是如果起点和终点相同时就要输出0,要分开讨论(是按我的代码,我对起点和终点分别赋值编号为1、2,这样的话当起点终点相同时就错了)
除了这个我还运行时错误,最后把char改成string,输入改成cin就没有了。。。。
这道题要把我气炸。。。。
下面是代码:
 #include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
const int INF=0xffffff;
struct shudui1
{
int start,value;
bool operator < (const shudui1 q)const
{
return value>q.value;
}
} str1;
struct shudui2
{
int start,value;
} str2;
priority_queue<shudui1>r;
map<string,int>t;
vector<shudui2>w[];
int d[],dis[],n;
void JK()
{
memset(dis,,sizeof(dis));
while(!r.empty())
{
str1=r.top();
r.pop();
int x=str1.start;
if(dis[x]) continue;
dis[x]=;
int len=w[x].size();
for(int i=; i<len; ++i)
{
str2=w[x][i];
if(!dis[str2.start] && d[str2.start]>d[x]+str2.value)
{
// printf("**\n");
str1.value=d[str2.start]=d[x]+str2.value;
str1.start=str2.start;
r.push(str1);
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
if(n==-) break;
int flag=,z;
string ss1,ss2,s1,s2;
cin>>ss1>>ss2;
t[ss1]=++flag;
t[ss2]=++flag;
for(int i=; i<=n; ++i)
{
int x,y;
cin>>s1>>s2>>z;
if(t[s1])
{
x=t[s1];
}
else
{
t[s1]=++flag;
x=flag;
}
if(t[s2])
{
y=t[s2];
}
else
{
t[s2]=++flag;
y=flag;
}
str2.start=y;
str2.value=z;
w[x].push_back(str2);
str2.start=x;
w[y].push_back(str2);
}
if(ss1!=ss2)
{
str1.start=;
str1.value=;
for(int i=; i<=; ++i)
d[i]=INF;
d[]=;
r.push(str1);
JK();
if(d[]==INF) printf("-1\n");
else printf("%d\n",d[]);
}
else printf("0\n");
for(int i=; i<=n; ++i)
w[i].clear();
t.clear();
}
return ;
}
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

InputFirst line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.

OutputFor each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."

Sample Input

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

13
题意:
就是我们要从r到a,只有“#”不可以走,其他都可以,但是过x的时候要注意,过x要先把x这个眼给打了,才能过去,也就是过这个点需要时间比平常多一
这道题我想到了三种方法,但是有一种错了
先说对的:
一、
用优先队列
就是bfs从a走到r,因为r可能有多个,所以不能从r走,然后就是走到x的时候要把时间多加一,然后用优先队列来对时间从小到大排序,这个样子不就满足题意了嘛,
只要我们走到r的位置,就直接跳出来
二、
普通BFS
我们走到x的前一个位值,就先把他杀死再进去,具体代码实现就是,之让这个点的时间加一,但是他的位置不变,在地图上面要把这一个点的值改为“.”
错的方法:
普通BFS
但是一直在迷宫里走,遇到x就加二,,一旦遇到r要记录这个时间,不要直接输出,最后走完迷宫再输出
错误地方:题目上面说是把守卫打死,那就是说x再经过一次之后,这个地方就没有守卫了。。。。。
代码如下:
优先队列的
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; int n, m;
char map[][];
int sx, sy;
bool flag;
struct node {
int x, y, step;
bool operator <(const node & t) const
{
return step>t.step;
}
};
int dx[]= {-,,,};
int dy[]= {,-,,}; void bfs() {
node now, tmp;
int i,xx,yy;
priority_queue<node> q;
now.x = sx, now.y = sy, now.step = ;
map[sx][sy] = '#';
q.push(now);
while(!q.empty()) {
now = q.top();
q.pop();
// cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
for(i=; i<; i++) {
xx = now.x +dx[i];
yy = now.y +dy[i];
if(xx<||xx>=n||yy<||yy>=m||map[xx][yy]=='#') continue;
if(map[xx][yy]=='r') {
cout<<now.step+<<endl;
flag = true;
return ;
}
if(map[xx][yy]=='x') {
tmp.x =xx, tmp.y = yy, tmp.step = now.step+;
q.push(tmp);
} else {
tmp.x =xx, tmp.y = yy, tmp.step = now.step+;
q.push(tmp);
}
map[xx][yy] = '#';
}
}
} int main() {
int i, j;
while(~scanf("%d%d",&n,&m)) {
for(i=; i<n; i++)
for(j=; j<m; j++) {
cin>>map[i][j];
if(map[i][j]=='a')
sx=i,sy=j;
}
flag = false;
bfs();
if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return ;
}

F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)的更多相关文章

  1. hdu 2112 (最短路+map)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)  ...

  2. hdu 2112 最短路

    本来是拿来复习一下map的,没想搞了半天,一直wa,最后发现预处理没有处理到所有的点 就是个最短路 Sample Input 6 xiasha westlake xiasha station 60 x ...

  3. HDU 2544 最短路 http://acm.hdu.edu.cn/showproblem.php?pid=2544

    //代码: //方法1:Dijkstra's Algorithm #include<stdio.h> #include<math.h> #include<string.h ...

  4. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  6. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  7. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  8. hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...

  9. HDOJ(HDU).2266 How Many Equations Can You Find (DFS)

    HDOJ(HDU).2266 How Many Equations Can You Find (DFS) [从零开始DFS(9)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零 ...

随机推荐

  1. 'python'不是内部或外部命令,也不是可运行程序或批处理文件

    配置两个环境变量: 我的电脑——属性——高级系统设置——环境变量——用户变量——path(新建) 1.配置python\python.exe所在的路径       path新建:C:\Users\Py ...

  2. EntityFrameworkCore中的OnModelCreating

    在我们使用EntityFrameworkCore作为数据库ORM框架的时候,不可避免的要重载DbContext中的一个虚方法OnModelCreating,那么这个方法到底是做什么的?到底有哪些作用呢 ...

  3. Python——pyqt5——智能提示(lineEdit/conmbobox)

    一.文本框智能补全 completer = QtWidgets.QCompleter(data) completer.setCompletionMode(QtWidgets.QCompleter.Po ...

  4. 火眼发布Windows攻击工具集

    导读 渗透测试员的喜讯:安全公司火眼发布Windows攻击工具集--足足包含140个程序. Kali Linux 已成为攻击型安全专家的标配工具,但对需要原生Windows功能的渗透测试员来说,维护良 ...

  5. ios端阻止页面滚动露底

    转自 http://www.eboy.me/archives/129: 在IOS端的微信中使用H5页面,页面滑动到底部时,再向上拉或页面在顶部时下拉,总会露出微信自带的底色:总是会让人不爽. 以下是一 ...

  6. Linux系统 Cetos 7 中重置root密码

    几个月前在自己电脑上面安装了一个Linux 的虚拟机环境,当时是为了测试某一个小功能,用完就扔那里了,长时间没有使用,发现Root密码忘记了,登陆不了,怎么办呢?(ps:如果实际情况中忘记密码的这个服 ...

  7. Verilog语言实现并行(循环冗余码)CRC校验

    1 前言 (1)    什么是CRC校验? CRC即循环冗余校验码:是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错功能, ...

  8. Python 目录指引

    1.0 Python 基础整合 1.1 变量 1.2 数据类型 1.3 基础语法 1.4 文件操作 1.5 函数 1.6 生成器 1.7 迭代器 1.8 装饰器 1.9 字符集 2.0 Python ...

  9. django rest framework renderer

    渲染器 REST framework 包含许多内置的渲染器类,允许您使用各种 media type 返回响应.同时也支持自定义渲染器. 视图的渲染器集合始终被定义为类列表.当调用视图时,REST fr ...

  10. [NOIP2017] 列队(平衡树)

    考虑转化题意: 设这次操作删掉点\((x, y)\) 对于每一次向左看齐:在第x行删除\((x, y)\),并将y以后的点全部前移一位 对于每一次向前看齐:x以后的点全部上移一位,并在最后一列插入\( ...