POJ1847 Tram【Dijkstra+思維】

Enjoy_process發表於2018-10-20

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;
}
 

 

相關文章