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 [从零 ...
随机推荐
- Django-6 Django ORM层
ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...
- 洛谷P2756 飞行员配对方案问题
二分图裸题,找他的最大匹配即可 #include<bits/stdc++.h> using namespace std; int n,m,ans; ; int to[N]; struct ...
- [ffmpeg] 定制滤波器
如果有定制ffmpeg滤波器的需求,有两个结构体是必须要了解的:AVFilter.AVFilterPad,所定制的滤波器主要就是通过填充这两个结构体来实现的.我们下面将详细解析这两个结构体,并通过对滤 ...
- 【pytorch】pytorch-LSTM
pytorch-LSTM() torch.nn包下实现了LSTM函数,实现LSTM层.多个LSTMcell组合起来是LSTM. LSTM自动实现了前向传播,不需要自己对序列进行迭代. LSTM的用到的 ...
- 基于Android P系统对selinux相关整理
1.首先selinux是一种加强文件安全的一种策略.主要包含进程和文件对象. 在system\sepolicy\public\attributes文件中有: # All types used for ...
- Tomcat启动报错,报找不到gdk_custom.jar
在 tomcat/conf/context.xml 中新增如下配置 <Context> ... <JarScanner scanManifest="false"/ ...
- jquery 设置 transform/translate 获取 transform/translate 的值
//获取 transform 值 var reg=/matrix.(((-)?([0-9]+.)?\d+([, ]+)?){6})./g; var str= progressUI.css(" ...
- Docker proxy
Method One: mkdir /etc/systemd/system/docker.service.dvim /etc/systemd/system/docker.service.d/http- ...
- JavaEEspring整理
Spring框架—控制反转(IOC) 1 Spring框架概述 1.1 什么是Spring 1.2 S ...
- my live thinkcenter / ThinkCentre M920x Tiny / Thinkpad yoga 12 vPro
s 025-58816312 联想3C服务中心:栖霞区学海路鸿运家园1栋6室 / 珠江路华海大厦8楼联想服务中心 营业时间:周一至周日,9:00∼18:00 ThinkPad Yoga 12 i7 v ...