您的位置:首页 > 其它

Dijkstra-POJ-1847-Tram

2015-10-18 19:13 337 查看
Tram

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 12338 Accepted: 4497

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

都说这道题的题意比较坑,要认真读题,那我就翻译一下题意吧。

一张铁轨网络由交叉点和铁轨构成,对于每一个交叉点,用选择器来控制指向所有出路中的一条,并且当火车到达交叉点时,只能顺着选择器指向的那条路行驶出交叉点,如果司机想要去该交叉点的另一个方向,那么他必须改变选择器。

求的是,当司机想要从第A个交叉点到第B个交叉点,所需的最小的改变选择器的次数是多少。

需要注意的是,在输入时,每个交叉点输入的第一个连接点,是选择器默认的指向方向,也就是说如果要从该交叉点到第一个连接点,需要的改变次数是0。

还需要注意的一点是,无论是让选择器选择第2个还是第3个还是第n个连接点,只要不是第一个,那么改变次数就是1,而不是n-1。

处理好这些问题后,一个dijkstra轻松愉快地解决。

//
//  main.cpp
//  最短路练习-N-Tram
//
//  Created by 袁子涵 on 15/10/18.
//  Copyright © 2015年 袁子涵. All rights reserved.
//

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define MAXIN 105
#define INF 0xffff
#define minof(a,b)  ((a)>(b)?(b):(a))

using namespace std;

int N,A,B;
bool visit[MAXIN];
int dis[MAXIN],map[MAXIN][MAXIN];

void dijkstra(int num)
{
for (int i=1; i<=N; i++) {
dis[i]=map[num][i];
}
visit[num]=1;
for (int i=1; i<=N; i++) {
int mins=INF,next=-1;
for (int j=1; j<=N; j++) {
if (visit[j]==0 && mins>dis[j]) {
mins=dis[j];
next=j;
}
}
if (next==-1) {
break;
}
visit[next]=1;
for (int j=1; j<=N; j++) {
if (visit[j]==0 && dis[j]>dis[next]+map[next][j]) {
dis[j]=dis[next]+map[next][j];
}
}
}
}

int main(int argc, const char * argv[]) {
int s,p;
cin >> N >> A >> B;
memset(visit, 0, sizeof(visit));
for (int i=1; i<=N; i++) {
for (int j=1; j<=N; j++) {
map[i][j]=INF;
}
map[i][i]=0;
}
for (int i=1; i<=N; i++) {
cin >> s;
for (int j=0; j<s; j++) {
cin >> p;
if (j==0) {
map[i][p]=0;
}
else
map[i][p]=minof(map[i][p],1);
}
}
dijkstra(A);
if (dis[B]!=INF) {
cout << dis[B] << endl;
}
else
cout << "-1" << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: