poj 1847 最短路 dijkstra模板(vector邻接表+队列优化)
来源:互联网 发布:dz seo 编辑:程序博客网 时间:2024/06/08 09:56
题目:
Tram
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 14423 Accepted: 5332
Description
Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch.
When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually.
Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B.
When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually.
Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B.
Input
The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are numbered from 1 to N.
Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed.
Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed.
Output
The first and only line of the output should contain the target minimal number. If there is no route from A to B the line should contain the integer "-1".
Sample Input
3 2 12 2 32 3 12 1 2
Sample Output
0
样例注释:
3 2 1 //有3个开关点,计算从第二个到第一个最少需要旋转几次
2 2 3 //第1个开关可以通向2个开关,通向2不需要旋转,通向3需要旋转1次
2 3 1 //第2个开关可以通向2个开关, 通向3不需要旋转,通向1需要旋转1次
2 1 2 //第3个开关可以通向2个开关, 通向1不需要旋转,通向2需要旋转1次
代码:
#include <stdio.h>#include <iostream>#include <queue>#include <vector>#include <algorithm>#include <math.H>#include <limits.h>using namespace std;const int maxn=109;const int inf=INT_MAX;typedef pair<int,int> p;struct edge{int to;int cost;};int n,start,end,d[maxn];vector<edge> g[maxn];void dijkstra(int s){priority_queue<p,vector<p>,greater<p> >qu;//堆按照p的first(最短距离)排序,小在前 fill(d,d+maxn,inf);d[s]=0;qu.push(p(0,s));//从起点出发到顶点s的最短距离为0while(!qu.empty()){p temp=qu.top();qu.pop();if(temp.first>d[temp.second]) continue;//跳过更新过程中入队的非最小值 for(int i=0;i<g[temp.second].size();++i){//遍历该顶点连出的每条边 edge e=g[temp.second][i];if(d[e.to]>d[temp.second]+e.cost){d[e.to]=d[temp.second]+e.cost;qu.push(p(d[e.to],e.to));}}}}int main(){int t,a;scanf("%d%d%d",&n,&start,&end);for(int i=1;i<=n;++i){//顶点编号从1到N scanf("%d",&t);if(!t) continue;scanf("%d",&a);edge e;e.to=a;e.cost=0;g[i].push_back(e);//从i到a权值为零 for(int j=1;j<t;++j){scanf("%d",&a);e.to=a;e.cost=1;g[i].push_back(e);//权值为 1 }}dijkstra(start);if(d[end]!=inf) printf("%d\n",d[end]);else puts("-1"); return 0;}
0 0
- poj 1847 最短路 dijkstra模板(vector邻接表+队列优化)
- 最短路模板(dijkstra+邻接表)
- 最短路 + 邻接表 + 堆优化(模板)(Dijkstra + SPFA)
- [POJ](3255)Roadblocks---- 最(次)短路径(邻接表+dijkstra优先队列优化)
- HDU 2544 最短路(单源最短路 dijkstra / floyd / dijkstra(堆优化)/dijkstra+邻接链表+堆优化)
- HDU 2544 最短路 静态邻接表+优先队列 dijkstra
- hdu 2544 最短路(Dijkstra 邻接表+优先队列)
- uva 11374 最短路+记录路径 好题 dijkstra优先队列优化算法 邻接表法 可做模板 G++提交
- 最短路模板——dijkstra,SPFA(邻接表实现)
- 【原创】最短路模板 Floyd,优先队列优化dijkstra,SPFA
- POJ 1724 ROADS 最短路 邻接表 + bfs +优先队列
- 最短路径Dijkstra(静态邻接表+优先队列模板)+ 记忆化搜索
- ACM:最短路,dijkstra,邻接表的建立,使用邻接表跟优先队列的dijkstra,Bellman-Ford,Floyd。。
- UVa 10986 Sending email (最短路+Dijkstra队列优化)
- 【poj 1724】 ROADS 最短路(dijkstra+优先队列)
- 最短路~dijkstra堆优化模板
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
- Dijkstra + 优先队列 + 邻接表优化
- hduoj1231 注意和1003的区别
- Angular中的缓存
- Video Cards
- java网络编程
- CSS3 Flex 弹性布局教程
- poj 1847 最短路 dijkstra模板(vector邻接表+队列优化)
- SQL语言-数据更新操作
- java基础常见面试题
- day05HTML DOM内置对象
- ActiveMQ SSL应用之二 使用keytool工具生成密钥和证书文件
- 稳定与未知,你到底会选择哪一个?
- html5 — JSON的新方法
- Ubuntu Bash and Dash
- 二.Contiki之事件