当前位置:网站首页>(POJ - 1847) tram (shortest circuit)
(POJ - 1847) tram (shortest circuit)
2022-06-27 17:24:00 【AC__ dream】
Topic link :1847 -- Tram
The question : Yes n A little bit , Then these points and other points have some paths , Each point is a switch , The switch can only go one way in one direction , The first number is the default switch pointing to , No rotation . It's the default pointing. In fact, you only need to rotate 0 Time , Other paths only need to rotate 1 Time , Whatever it is , just 1 Time . seek a To b Minimum number of rotations
This problem is a bare template with the shortest path , But the length of the edge he gave was only 0 and 1, Is that the distance of the first point given is 0, The distance between other points is 1, This place has stuck me for a long time , At first, I thought that the current point was the number and the given distance was the number -1, In fact, as long as it is not the first one to give , Then the distance is 1, This place needs attention , The rest of the set of the shortest path template will pass , Put the code below :
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1003;
int h[N],w[N*N],ne[N*N],e[N*N],idx,d[N];
bool vis[N];
int n,b,en;
typedef pair<int,int>PII;
void add(int x,int y,int z)
{
e[idx]=y;
w[idx]=z;
ne[idx]=h[x];
h[x]=idx++;
}
void dijkstra(int x)
{
memset(d,0x3f,sizeof d);
priority_queue<PII,vector<PII>,greater<PII> >q;
d[x]=0;
q.push({d[x],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});
}
}
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>b>>en;
for(int i=1;i<=n;i++)
{
int cnt,t;
scanf("%d",&cnt);
for(int j=1;j<=cnt;j++)
{
scanf("%d",&t);
if(j==1) add(i,t,0);
else add(i,t,1);
}
}
dijkstra(b);
if(d[en]==0x3f3f3f3f) puts("-1");
else printf("%d",d[en]);
return 0;
}边栏推荐
- 软件测试学习-黑马程序员,软件测试学习大纲
- Some details of Huawei OSPF
- Handling method of occasional error reporting on overseas equipment
- 阿里云刘珅孜:云游戏带来的启发——端上创新
- Oracle concept II
- National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
- Dark horse programmer - software testing foundation class -02-30-45 tools open browser running code, audio, video, test points, audio and video labels, layout labels. Advanced hyperlink syntax, absolu
- Sword finger offer 22 The penultimate node in the linked list
- Leetcode 5. Longest Palindromic Substring
- Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
猜你喜欢

米哈游起诉五矿信托,后者曾被曝产品暴雷
Yyds dry inventory brief chrome V8 engine garbage collection

软件测试学习-黑马程序员,软件测试学习大纲

Leetcode 5. Longest Palindromic Substring
P. Simple application of a.r.a method in Siyuan (friendly testing)

Autodesk NavisWorks 2022 software installation package download and installation tutorial

全面解析零知识证明:消解扩容难题 重新定义「隐私安全」

What is RPC

Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)

Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
随机推荐
Defiato is an innovation that combines user-friendly features of a centralized platform with defi services
10 minutes to master the installation steps of MySQL
06. First introduction to express
Part 32 supplement (32) string of ECMAScript
About how vs2019c # establishes the login interface, the user name and password entered must match the records in the access database
米哈游起诉五矿信托,后者曾被曝产品暴雷
Autodesk Navisworks 2022软件安装包下载及安装教程
C système de gestion de la charge de travail des enseignants en langues
如何提升IT电子设备效能管理
Popularization of MCU IO port: detailed explanation of push-pull output and open drain output
关于#mysql#的问题:问题遇到的现象和发生背景
leetcode 69. Square root of X
Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'
What is RPC
Under the influence of external factors, how to manage internal resources and reduce costs is the key
Generate zip package command
#yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
Extract field year / quarter effect based on date
Kubernetes基础自学系列 | Ingress API讲解
2/14 preliminary calculation geometry