POJ1847 Tram【Dijkstra+思维】

Tram

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 19612   Accepted: 7339

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

Source

Croatia OI 2002 Regional - Juniors

问题链接:POJ1847 Tram

问题描述:有轨电车轨道有N个交汇点(从1开始编号),交汇点是由整数K及其后K个整数描述,K表示此交汇点可通往的K个交汇点,其后的K个整数表示可通向的交汇点的编号,一次只能通向一个交汇点,第一个数表示初始通向的交汇点,如果司机想要去其他交汇点就必须手动切换一次,问从A到B最少转换的次数,如果无法达就输出-1

解题思路:问题的关键是如何建边,边的权值只有1和0两种情况,0是交汇点初始通向的初始交汇点的边,1是剩下的其他轨道。建边完成后就可以使用最短路算法进行求解,程序使用Dijkstra算法

AC的C++程序:

#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>

using namespace std;

const int N=105;
const int INF=0x3f3f3f3f;
int dist[N];
int g[N][N];
bool vis[N];

struct Node{
	int u,w;
	//Node(){}
	Node(int u,int w):u(u),w(w){}
	bool operator<(const Node &a)const
	{
		return w>a.w;
	} 
};

void dijkstra(int s,int n) 
{
	memset(dist,INF,sizeof(dist));
	memset(vis,false,sizeof(vis));
	dist[s]=0;
	priority_queue<Node>q;
	q.push(Node(s,0));
	while(!q.empty()){
		Node f=q.top();
		q.pop();
		int u=f.u;
		if(!vis[u]){
			vis[u]=true;
			for(int i=1;i<=n;i++)
			  if(!vis[i]&&g[u][i]!=INF&&dist[i]>dist[u]+g[u][i]){ 
			  		dist[i]=dist[u]+g[u][i];
			  		q.push(Node(i,dist[i]));
			  }
		}
	}
}

int main()
{
	int n,a,b,k,x;
	scanf("%d%d%d",&n,&a,&b);
	memset(g,INF,sizeof(g));
	for(int i=1;i<=n;i++){
		scanf("%d",&k);
		for(int j=1;j<=k;j++){
			scanf("%d",&x);
			g[i][x]=(j==1)?0:1;
		}
	}
	dijkstra(a,n);
	if(dist[b]==INF)
	  printf("-1");
	else
	  printf("%d\n",dist[b]);
	return 0;
}
 

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/83216496
今日推荐