POJ 1847 Tram (最短路径)

POJ 1847 Tram (最短路径)

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

Http

POJ:https://vjudge.net/problem/POJ-1847

Source

最短路径

题目大意

有n个车站,这每一个车站都有若干条出口,开始时指定一个出口,如果走这这个出口,就不要付出代价,否则要付出1的代价,求从1到n的最小代价

解决思路

其实就是最短路径。把指定的出口的边的边权置为0,其他置为1,然后求最短路径即可。
注意无解的情况

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

const int maxN=201;
const int maxM=maxN*maxN;
const int inf=2147483647;

int n;

class Graph
{
private:
	int cnt;
	int Head[maxN];
	int W[maxM];
	int Next[maxM];
	int V[maxM];
	//priority_queue<Data,vector<Data>,greater<Data> > Q;
	queue<int> Q;
	bool inqueue[maxN];
public:
	int s,t;
	int Dist[maxN];
	void init()
		{
			cnt=0;
			memset(Head,-1,sizeof(Head));
			memset(Next,-1,sizeof(Next));
		}
	void Add_Edge(int u,int v,int w)
		{
			cnt++;
			Next[cnt]=Head[u];
			Head[u]=cnt;
			V[cnt]=v;
			W[cnt]=w;
		}
	void spfa()
		{
			while (!Q.empty())
				Q.pop();
			for (int i=1;i<=n;i++)
				Dist[i]=inf;
			memset(inqueue,0,sizeof(inqueue));
			Dist[s]=0;
			Q.push(s);
			inqueue[s]=1;
			do
			{
				int u=Q.front();
				Q.pop();
				for (int i=Head[u];i!=-1;i=Next[i])
				{
					if (Dist[V[i]]>Dist[u]+W[i])
					{
						Dist[V[i]]=Dist[u]+W[i];
						if (inqueue[V[i]]==0)
						{
							Q.push(V[i]);
							inqueue[V[i]]=1;
						}
					}
				}
				inqueue[u]=0;
			}
			while (!Q.empty());
			return;
		}
	void Outp()
		{
			for (int i=1;i<=n;i++)
			{
				for (int j=Head[i];j!=-1;j=Next[j])
				{
					cout<<"("<<i<<","<<V[j]<<") "<<W[j]<<endl;
				}
				cout<<endl;
			}
		}
};

Graph G;

int main()
{
	G.init();
	cin>>n>>G.s>>G.t;
	for (int i=1;i<=n;i++)
	{
		int T;
		cin>>T;
		int v;
		cin>>v;
		G.Add_Edge(i,v,0);
		if (T<2)
			continue;
		for (int j=2;j<=T;j++)
		{
			cin>>v;
			G.Add_Edge(i,v,1);
		}
	}
	//G.Outp();
	G.spfa();
	if (G.Dist[G.t]==inf)
		cout<<-1<<endl;
	else
		cout<<G.Dist[G.t]<<endl;
	return 0;
}
posted @ 2017-07-29 13:47  SYCstudio  阅读(202)  评论(0编辑  收藏  举报