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. 

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. 

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