当前位置:网站首页>(2022牛客多校三)J-Journey(dijkstra)
(2022牛客多校三)J-Journey(dijkstra)
2022-08-08 23:06:00 【AC__dream】
题目:
样例输入:
4
3 4 0 0
0 0 4 3
2 1 0 0
2 0 0 1
4 2 4 1
样例输出:
1
题意:给定一个城市有若干个十字路口,右转需要不需要等红灯,直行、左转和掉头都需要等活动,要求从起点到终点最少要等多少次红灯。
分析:这道题我们可以把每条带方向的边看成一个点,注意一条边的两个方向看成是两个不同的点,处理好这些东西之后我们就可以用dijkstra跑一个最短路就可以了
重要的是怎么建图,对于一个边如果之前没有记录过我们可以直接把其看成一个点,但是我们怎么知道当前点到其他点之间的边是权值为1还是为0,这个就需要画图思考一下,比如我们现在与t点相邻的一个路口为1号十字路口,那么假如(t,1)是t路口的第一个边,那么我们可以找出来(1,t)是1号十字路口的第几条边,那么当前边的下一条边就是(t,1)右转的边,那么就将这两条边建一条权值为0的边即可,注意我们是把边抽象化为点,且建立的边是单向边,至于1号十字路口的其余三条边我们将(t,1)与其它三条边建立一条长度为1的边即可,就这样我们就完成建图,然后从起点跑一个最短路就可以求出答案。
细节见代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<unordered_map>
using namespace std;
typedef pair<int,int> PII;
#define int long long
unordered_map<long long,int>mp;
const int N=5e6+10,M=5e7+10;
int p[N][4];
int h[N],ne[M],e[M],w[M],idx;
int be,en,d[N];
bool vis[N];
void add(int x,int y,int z)
{
e[idx]=y;
w[idx]=z;
ne[idx]=h[x];
h[x]=idx++;
}
int dijkstra(int x)
{
memset(d,0x3f,sizeof(d));
d[x]=0;
priority_queue<PII,vector<PII>,greater<PII> >q;
q.push({0,x});
while(!q.empty())
{
int begin=q.top().second;
q.pop();
if(vis[begin]) continue;
vis[begin]=true;
for(int i=h[begin];i!=-1;i=ne[i])
{
int j=e[i];
if(d[j]>d[begin]+w[i])
{
d[j]=d[begin]+w[i];
q.push({d[j],j});
}
}
}
if(d[en]==0x3f3f3f3f3f3f3f3f) return -1;
return d[en];
}
signed main()
{
int n;
cin>>n;
int tt=0;
for(int i=1;i<=n;i++)
scanf("%lld%lld%lld%lld",&p[i][0],&p[i][1],&p[i][2],&p[i][3]);
for(int i=1;i<=4*n;i++) h[i]=-1;//注意这里的点的个数不是n,我们把每条边抽象为一个点
for(int i=1;i<=n;i++)
{
for(int j=0;j<4;j++)
{
int t=p[i][j];
if(t)
{
if(!mp[1ll*i*1e9+t]) mp[1ll*i*1e9+t]=++tt;
int k=0;
for(;k<4;k++)
if(p[t][k]==i) break;
k=(k+1)%4;
if(!mp[t*1e9+p[t][k]]) mp[t*1e9+p[t][k]]=++tt;
add(mp[i*1e9+t],mp[t*1e9+p[t][k]],0);
for(int l=1;l<4;l++)
{
k=(k+1)%4;
if(!mp[t*1e9+p[t][k]]) mp[t*1e9+p[t][k]]=++tt;
add(mp[i*1e9+t],mp[t*1e9+p[t][k]],1);
}
}
}
}
int a,b,c,d;
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
be=mp[a*1e9+b];
en=mp[c*1e9+d];
cout<<dijkstra(be);
return 0;
}
边栏推荐
- Introduction to Qt (5) - file operation, hotkey and mouse reading (implementation of txt window)
- (2022杭电多校六)1010-Planar graph(最小生成树)
- The second side of Tencent technical support internship - Tencent's father's luck is so sudden (offer received)
- Free ARP
- 待完善:tf.name_scope() 和 tf.variable_scope()的区别
- stm32 利用 串口接收空闲中断 + dma 实现不定长度dma 接收
- 从stm32移植ucos2的代码到GD32
- 三国战绩 风云再起 网络版 物品序号 和 基址列表
- PMP考点有哪些啊?
- 生活中无处不在的MPLS虚拟专用网
猜你喜欢
人人熟知的IPV6竟然还有这么多细节
Virtualization type (with picture)
树莓派wiringPi库的使用补充
动态主机配置协议DHCP(DHCPv4)
Qt入门(四)——连续播放图片(两种定时器的运用)
2022牛客多校六 B-Eezie and Pie (dfs)
sess.restore() 和 tf.import_meta_graph() 在使用时的一些关联
Qt入门(五)——文件操作、热键和鼠标的读取(txt窗口的实现)
(2022牛客多校五)D-Birds in the tree(树形DP)
Virtual router redundancy protocol VRRP - double-machine hot backup
随机推荐
php 将时间戳转化为 刚刚、几分钟前、几小时前、几天前 格式
Taro小程序跨端开发入门实战
按键精灵 删除文件 命令
C language library function summary2019.10.31
Free ARP
【Bug解决】ValueError: Object arrays cannot be loaded when allow_pickle=False
(2022牛客多校四)H-Wall Builder II(思维)
Share | design based on MCU P0 mouth to drive the LED flashing
数组去重的几种方法
Hi3516 use wifi module
机器学习之知识点(一)
应用层协议——RADIUS
你了解你每天都在用的NAT吗?
JS中的原型与原型链
线性筛求积性函数
从洞察到决策,一文解读标签画像体系建设方法论丨DTVision分析洞察篇
C语言 库函数汇总2019.10.31
wps表格怎么筛选出需要的内容?wps表格筛选出需要的内容的方法
flutter 书写json解析类
postman request+加密解密