POJ-1847 Tram

Tram

Time Limit: 1000MS   Memory Limit: 30000K

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

题解很简单的最短路问题,套版就行,建好图就行了,这里用未优化的Dijkstra就行了代码
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int maxn = 100+7;
const int inf=0x3f3f3f3f;

//*****************************************
//Dijkstra-数组实现O(n^2)
//单源最短路
//lowcost[]-----从点beg到其他点的距离
//*****************************************
bool vis[maxn];
void Dijkstra(int cost[][maxn],int lowcost[maxn],int n,int beg)
{
    int minc;
    int i,j,w;
    memset(vis,false,sizeof(vis));
    vis[beg]=true;
    for(i=1;i<=n;i++) lowcost[i]=cost[beg][i];
    lowcost[beg]=0;
    for(i=1;i<=n;i++){
        minc=inf;
        for(j=1;j<=n;j++){
            if(!vis[j]&&lowcost[j]<minc){
                minc=lowcost[j];
                w=j;
            }
        }
        if(minc>=inf) break;
        vis[w]=true;
        for(j=1;j<=n;j++)
            if(!vis[j]&&lowcost[w]+cost[w][j]<lowcost[j])
            lowcost[j]=lowcost[w]+cost[w][j];
    }
}

int cost[maxn][maxn];
int dist[maxn];
int main()
{
    int n,A,B;
    int k,t;
    while(scanf("%d%d%d",&n,&A,&B)!=EOF){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==j) cost[i][j]=0;
                else cost[i][j]=inf;
            }
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&k);
            for(int j=0;j<k;j++){
                scanf("%d",&t);
                if(j==0) cost[i][t]=0;
                else cost[i][t]=1;
            }
        }
        Dijkstra(cost,dist,n,A);
        if(dist[B]>=inf) printf("-1\n");
        else printf("%d\n",dist[B]);
    }
    return 0;
}
 
时间: 2024-08-11 01:23:25

POJ-1847 Tram的相关文章

POJ 1847 Tram 单源最短路径

题意:轨道网,有若干转换器,每个转换器都和其他若干转换器相连,转换器初始指向第一个与其相连的转换器.问要到达终点需要最少转换多少次? 思路:可以用dijkstra单源最短路来做,把轨道网看做有向图(因为1第一个指向2,2的第一个不一定指向1),当前转换器处始指向的那个转换器之间的路径权值为0,其他路径权值为1,求一次起点到终点的最短路,结果就是最少转换次数,注意可能没有路径,这时要输出-1 代码: /* poj 1847 264K 0MS */ #include<cstdio> #includ

POJ 1847 Tram 【最短路,spfa算法,题意理解是关键呀!!】

Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13468   Accepted: 4954 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 t

(简单) POJ 1847 Tram,Dijkstra。

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

poj 1847 Tram【spfa最短路】

Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 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 t

Floyd_Warshall POJ 1847 Tram

题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd跑一下,得到A到B最少转换几次.有点水 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 1e2 + 5; const int INF =

POJ 1847 Tram【Floyd】

题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start,end 接下来的n行,每一行中,第一个数是该站点向外连接的铁路条数, 第二个数是该站点的开关指向的铁路(因为这儿没有读懂= =所以都建不出图来--5555参见这一句话:Switch in the i-th intersection is initially pointing in the dire

poj 1847 最短路简单题,dijkstra

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

Tram POJ - 1847 spfa

#include<iostream> #include<algorithm> #include<queue> #include<cstdio> #include<cstring> using namespace std; const int N=1100,INF=0x3f3f3f3f; int h[N],e[N],v[N],w[N],ne[N],idx; int dist[N]; bool st[N]; int n,beg,en,ans; voi

poj 1847( floyd &amp;&amp; spfa )

http://poj.org/problem?id=1847 一个水题,用来熟悉熟悉spfa和floyd的. 题意:有m条的铁路,要从x,到y, 之后分别就是条铁路与其他铁路的交点.第一个输入的为有n个交点.之后第一个输入的点,当前铁路到这个点是不要转向的,也就是权值为0,其余的权值都为1,求从x到y的最短路,如果到不了输出-1 裸的floyd和spfa: 1 #include <stdio.h> 2 #include <string.h> 3 #define inf 0x3f3f

POJ 1847 dijstra算法

POJ 无限循环CE中.感觉是读题难.然后就可以建图上模板了. 附个人代码: #include<stdio.h>#include<string.h>#include<iostream>#define maxn 0x1f1f1f1f#define size 210using namespace std; int low[size];bool used[size];int map[size][size];int n, a, b; void init(){    for (i