当前位置:网站首页>Niuke multi school -journey- (map building distra+ card often optimization)
Niuke multi school -journey- (map building distra+ card often optimization)
2022-07-26 21:22:00 【Lovely and beautiful girl】
The question :
It's for you. n A crossroads , There are four directions at each intersection , Mark your intersection counterclockwise , If it is 0 The sign means there is no intersection . Then every time you turn right at the intersection, it doesn't cost , And move on , Turn left , Overturn , It's all cost 1. First, you are A The intersection faces B On the road at the intersection , Let you walk to C The intersection faces D On the road at the intersection . Ask you the least cost . The direction you face is unnecessary ,A Yes B and B Yes A It's different .
reflection :
In fact, this question , Give you the intersection , I didn't give you a designated direction , Just in reverse order . If you run the shortest way at the intersection, you will find this diagram difficult to get . So run by the side , That is, regard the edge as a point , Look at these edges as different points , Conduct hash. How did you think of it , Because it's about the cost of walking from one road to another , So it's easier to take the road as a point . When building a map, it is to enumerate four intersections of an intersection , Then find two intersections , Build a map for them , If it's a right turn, it doesn't cost .
Then it is worth noting that , use map<PII,int> Hash will timeout , Then change to unordered_map<PII,int,cmp>. This cmp Overload usage is different, there will be timeout and no timeout . Of course, the safest thing is not to build a map , Run straight , Just enumerate where you go when you run . When hashing, hash the edge into a particularly large value and put it in unordered_map<int,int> That's all right. .
Code :
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 5e6+10,M = 2010;
int T,n,m,k;
int va[N][4];
int dist[N],vis[N];
struct cmp{
int operator()(PII A)const{
return A.fi*521417+A.se;
}
};
int cnt;
unordered_map<PII,int,cmp> mp;
vector<PII > e[N];
void distra(int A)
{
for(int i=1;i<=cnt;i++) dist[i] = inf;
priority_queue<PII,vector<PII>,greater<PII> > q;
q.push({
0,A});
dist[A] = 0;
while(q.size())
{
auto t = q.top();q.pop();
int now = t.se,dis = t.fi;
if(vis[now]) continue;
for(auto tt:e[now])
{
int spot = tt.fi,w = tt.se;
if(dist[spot]>dist[now]+w)
{
dist[spot] = dist[now]+w;
q.push({
dist[spot],spot});
}
}
}
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=0;j<4;j++)
{
cin>>va[i][j];
if(!va[i][j]) continue;
int a = i,b = va[i][j];
if(!mp[{
a,b}]) mp[{
a,b}] = ++cnt;
if(!mp[{
b,a}]) mp[{
b,a}] = ++cnt;
}
for(int j=0;j<4;j++)
{
if(!va[i][j]) continue;
for(int k=0;k<4;k++)
{
if(!va[i][k]) continue;
int a = va[i][j],b = i,c = va[i][k],w = 1;
if((j+1)%4==k) w = 0;
int x = mp[{
a,b}],y = mp[{
b,c}];
e[x].pb({
y,w});
}
}
}
int A,B,C,D;
cin>>A>>B>>C>>D;
distra(mp[{
A,B}]);
if(dist[mp[{
C,D}]]==inf) cout<<-1;
else cout<<dist[mp[{
C,D}]];
return 0;
}
summary :
Try more , I feel map When it doesn't work ,unordered Not good either. , I don't want to write . Little by little optimization , Maybe it's over in the end .
边栏推荐
- How to implement Devops with automation tools | including low code and Devops application practice
- 【打新必读】工大科雅估值分析,供热节能产品
- Sprinboot interview questions
- 6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
- 【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复
- [JVM series] JVM tuning
- Daily practice ----- there is a group of students' grades. Arrange them in descending order. To add a student's grade, insert it into the grade sequence and keep the descending order
- Detailed illustration of B-tree and its implementation in C language
- 自定义注解(一)
- APaaS低代码平台(一) | 把复杂留给自己,把简单留给用户
猜你喜欢

基于Hough变换的直线检测(Matlab)

How to configure the legendary SF lander to automatically read the list without a network

Explain the 190 secondary compiler (decoding table) module of SMR laminated hard disk of Western data in detail

ECCV 2022 | 同时完成四项跟踪任务!Unicorn: 迈向目标跟踪的大统一

游览器——游览器游览器缓存

GOM跟GEE登陆器列表文件加密教程

2022开放原子全球开源峰会议程速递 | 7 月 27 日分论坛议程一览

Test cases should never be used casually, recording the thinking caused by the exception of a test case

In addition to "adding machines", in fact, your micro service can be optimized like this

Flash source code outline
随机推荐
【问题篇】将集合[‘‘,‘‘]处理成(‘‘,‘‘)
SSM整合实例
播报语音 h5 SpeechSynthesisUtterance
【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复
Why does it system need observability?
Today, the company came across an Alibaba P8, which was really seen as the ceiling of the foundation
Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro
[pytoch foundation] torch.stack() function analysis
Practice of microservice in solving Library Download business problems
Redis hash和string的区别
拦截器、、
[英雄星球七月集训LeetCode解题日报] 第26日 并查集
GOM and GEE lander list file encryption tutorial
ROS2节点通信实现零拷贝
Flutter Performance Optimization Practice - UI chapter
2022 open atom global open source summit agenda express | list of sub forum agenda on July 27
手机\固定电话座机呼叫转移设置方法
【HCIA安全】双向NAT
After chatting with byte programmers with a monthly salary of 3W, I realized that I had been doing chores
4年软件测试工作经验,跳槽之后面试20余家公司的总结