经过锦囊相助,海东集团终于度过了危机,从此,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. jdk自带的监测cpu/内存、线程等信息的工具

    Jvisualvm:jdk自带的监控工具(JDK1.6+) 在终端中输入Jvisualvm回车出现如下界面:

  2. win 执行puppet

    C:\scripts\win_exec_proxy.bat  \\adsoft.base-fx.com\puppet\puppet\files\Windows_10_x64\C\user\logon\ ...

  3. linux shell通配符及if语句判断

    $# 是传给脚本的参数个数 $0 是脚本本身的名字$1 是传递给该shell脚本的第一个参数$2 是传递给该shell脚本的第二个参数$@ 是传给脚本的所有参数的列表$* 是以一个单字符串显示所有向脚 ...

  4. Oracle 12c CDB PDB 安装/配置/管理

    Oracle安装参考:https://www.cnblogs.com/zhichaoma/p/9288739.html 对于CDB,启动和关闭与之前传统的方式一样,具体语法如下:     STARTU ...

  5. k-均值聚类算法1

    一.k-means算法: 1.优缺点: 优点:容易实现. 缺点:可能收敛到局部最小值,在大规模数据集上收敛较慢. 2.伪代码描述:

  6. mac与windows共享键盘鼠标(synergy)

    桌面上有两台电脑, 一台mac一台windows, 由于桌面空间紧张, 放两套键盘鼠标有点浪费空间, 如果能让mac和windows共享键盘鼠标就好了, 经过一番搜寻, 找到了一款名为synergy的 ...

  7. 内核空间内存申请函数kmalloc kzalloc vmalloc的区别

    我们都知道在用户空间动态申请内存用的函数是 malloc(),这个函数在各种操作系统上的使用是一致的,对应的用户空间内存释放函数是 free().注意:动态申请的内存使用完后必须要释放,否则会造成内存 ...

  8. application对象的应用案例

    application对象由多个客户端用户共享,它的应用范围是所有的客户,服务器启动后,新建一个application对象,该对象一旦建立,就一直保持到服务器关闭.当有客户访问服务器上的一个JSP页面 ...

  9. [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [E:\soft\studySoft\tomcat\apache-tomcat-8.5.33\webapp

    问题 启动tomcat,就一直卡在了这里 继续往上查看日志 解决方法:

  10. LinkedBlockingQueue阻塞队列详解

    主要api java.util.concurrent包下的新类.LinkedBlockingQueue就是其中之一,是一个阻塞的线程安全的队列,底层采用链表实现.        LinkedBlock ...