1. 程式人生 > >poj 1847 最短路 dijkstra模板(vector鄰接表+佇列優化)

poj 1847 最短路 dijkstra模板(vector鄰接表+佇列優化)

題目:

Tram
Time Limit: 1000MS Memory Limit: 30000K
Total 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 1
2 2 3
2 3 1
2 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<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;

typedef pair<int,int> p;//<a,b> 起點到b的最短距離為a 

struct edge{
	int to;
	int cost;
	edge(int tto=0,int ccost=0):to(tto),cost(ccost){}
};

int n,m,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的最短距離為0
	while(!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);
		g[i].push_back(edge(a,0));//從i到a權值為零 
		for(int j=1;j<t;++j){
			scanf("%d",&a);
			g[i].push_back(edge(a,1));//權值為 1 
		}
	}
	dijkstra(start);
	if(d[end]!=inf) printf("%d\n",d[end]);
	else puts("-1"); 
	return 0;
}