F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“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'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)的更多相关文章
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- hdu 2112 最短路
本来是拿来复习一下map的,没想搞了半天,一直wa,最后发现预处理没有处理到所有的点 就是个最短路 Sample Input 6 xiasha westlake xiasha station 60 x ...
- HDU 2544 最短路 http://acm.hdu.edu.cn/showproblem.php?pid=2544
//代码: //方法1:Dijkstra's Algorithm #include<stdio.h> #include<math.h> #include<string.h ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- 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 [从零 ...
随机推荐
- springboot使用RestHighLevelClient批量插入
1.引入maven(注意版本要一致) <dependency> <groupId>org.springframework.boot</groupId> <ar ...
- bind:tap="onLike" 中不能添加参数 及 dataset 传值
<view bind:tap="onLike({{id}})"></view> //Component "pages/book-detail/bo ...
- ionic3创建选项卡
html页面 <ion-content padding> <ion-segment [(ngModel)]="tabs"> <ion-segment- ...
- 【CSA35G】【XSY3318】Counting Quests DP 拉格朗日反演 NTT
题目大意 zjt 是个神仙. 一天,zjt 正在和 yww 玩猜数游戏. zjt 先想一个 \([1,n]\) 之间的整数 \(x\),然后 yww 开始向他问问题. yww 每次给 zjt 一个区间 ...
- Vim内直接使用p粘贴系统剪切板
解决方法 set clipboard=unnamed
- 基于Redis缓存几十万条记录的快速模糊检索的功能实现(c#)
在开发一套大型的信息系统中,发现很多功能需要按公司查询,各个模块在实现时都是直接查数据库进行模糊检索,虽然对表进行向各个应用的库中进行了同步,但是在使用中发现,模糊检索公司时还是比较卡,原始的查询数据 ...
- C#面向对象基本概念总结
快过年了,发一篇自己的复习总结.以下内容均是个人理解,如文章有幸被浏览,如有错误的地方欢迎大家提出,相互学习相互进步! 面向对象三大基本特征:封装,继承,多态 一.类 (对象声明的三种方式:以普通基类 ...
- Python爬虫之三
1)使用Scrapy,什么叫做Scrapy Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据 ...
- linux/ubantu 安装 mysql 并且使其支持远程连接
前言:打开ubantu的 控制台 或者 远程连接到 ubantu的服务器 其他的 linux 基本类似 可能下载 方式稍微不同 开始吧! [第一步]首先是安装(目前是2019-4-9 默认安装的是 ...
- 开启telnet
title: 开启telnet data; 2019/3/19 17:35:33 --- 开启telnet 添加root用户 adduser root 按照提示新增文件 /etc/passwd /et ...