POJ - 1847Tram最短路

POJ - 1847

Tram

Time Limit: 1000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u

Submit Status

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
/*
 *Author: 2486
 *Memory: 716 KB    Time: 16 MS
 *Language: G++     Result: Accepted
 */
//理解题目意思,看懂题目即可
//其中注意细节,开关最开始的方向是由第一个数的值所确定的,所以对于第一个数的
//状态是不需要进行开关操作的
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 100 + 5;
int n,a,b,x,y;
int d[N][N];

int warshall_floyd() {
    for(int  i = 0 ; i <= n; i ++) {
        d[i][i] = 0;
    }
    for(int k = 1; k <= n; k ++) {
        for(int i = 1 ; i <= n; i ++) {
            for(int j = 1; j <= n ; j ++) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

int main() {
    while(~scanf("%d%d%d",&n,&a,&b)) {
        memset(d,0x3f,sizeof(d));
        for(int i = 1; i <= n; i ++) {
            scanf("%d",&x);
            for(int j = 0; j < x; j ++) {
                scanf("%d",&y);
                if(j) d[i][y] = 1;
                else  d[i][y] = 0;
            }
        }
        warshall_floyd();
        if(d[a][b] != INF)printf("%d\n",d[a][b]);
        else printf("-1\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 19:03:21

POJ - 1847Tram最短路的相关文章

poj 1502 最短路+坑爹题意

链接:http://poj.org/problem?id=1502 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5249   Accepted: 3237 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed share

POJ 2448(K短路,A*+SPFA) Remmarguts&#39; Date

题意 给一个n个点m条边的图,然后给一个起点和一个终点,求起点到终点的第K短路. 思路 求第K短路.一个经典的问题. SPFA+A* 核心思想在A*搜索的估计函数的建立上. F(x) = g(x) + h(x) 估价函数 = s到x的距离 + x到t的距离 估价函数的含义就是经过x这个点的路径的距离. 我们在搜索的时候每次选择估价函数较小的值,进行拓展.这样我们搜索到t点的状态出来顺序就是,最短路-次短路-.第三短路- 就减少了我们搜索的状态数. 代码实现上,实现一个估价函数的结构体,然后是实现

poj 1135 最短路 dijkstra

传送门 http://poj.org/problem?id=1135 建模分两部分:1.如果最后是关键牌倒下,那么找最短路中最长的就行--最远的倒下,其他的牌一定倒下,所以找最远的最短路 2.如果最后是普通牌倒下,那么找三角形,三角形周长的一半就是倒下的位置 到底是情况1还是情况2,自己在脑子模拟一下就能想到,还是那句话,最难倒下的倒下了,其他的一定都倒下了,若第二种情况的时间比第一种长,那么就是第二种,反之,第一种 上代码 /**********************************

It&amp;#39;s not a Bug, It&amp;#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1353   Accepted: 516 Description It is a curious fact that consumers buying a new software product generally do not expect the software to

Heavy Transportation POJ 1797 最短路变形

Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你求从编号为1的城市到编号为n的城市的路线中,最大能经过多重的车. 解题思路 这个题可以使用最短路的思路,不过转移方程变了\(dis[j]=max(dis[j], min(dis[u], e[u][j]))\).这里dis[j]表示从标号为1的点到达编号为j的点的路径中,最小的承重能力,就像短板效应样

poj 1847 最短路简单题,dijkstra

1.poj  1847  Tram   最短路 2.总结:用dijkstra做的,算出a到其它各个点要改向的次数.其它应该也可以. 题意: 有点难懂.n个结点,每个点可通向ki个相邻点,默认指向第一个相邻点,可以改变指向.求一条从A到B的路,使用最少改变路上点的指向的次数. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm>

poj 2387 最短路 spfa 实现

http://poj.org/problem?id=2387 题目大意就是求最短路,从n到1的最短路.就用来熟悉一下spfa的写法. 一开始贡献了好几次wa,结果发现是因为n,m写反了.... 没有什么拐弯的地方,来熟悉spfa直接附上代码: #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> using names

POJ 1797 最短路

链接: http://poj.org/problem?id=1797 题意: 给出N个城市M条边,每条边都有个容量,求一条运输路线,使城市1到城市N的运输量最大 代码: 31 int cost[MAXN][MAXN]; 32 int d[MAXN]; 33 int used[MAXN]; 34 int n, m; 35 36 void dijkstra() { 37 memset(d, 0, sizeof(d)); 38 memset(used, 0, sizeof(used)); 39 d[0

poj 3463 最短路+次短路

独立写查错不能,就是维护一个次短路的dist 题意:给定一个有向图,问从起点到终点,最短路+比最短路距离长1的路的个数. Sample Input25 81 2 31 3 21 4 52 3 12 5 33 4 23 5 44 5 31 55 62 3 13 2 13 1 104 5 25 2 75 2 74 1 Sample Output32 2015-05-14 1 #include<cstdio> 2 #include<iostream> 3 #include<algo