POJ 1847 Tram (最短路径)

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

Http

POJ:https://vjudge.net/problem/POJ-1847

Source

最短路径

题目大意

有n个车站,这每一个车站都有若干条出口,开始时指定一个出口,如果走这这个出口,就不要付出代价,否则要付出1的代价,求从1到n的最小代价

解决思路

其实就是最短路径。把指定的出口的边的边权置为0,其他置为1,然后求最短路径即可。

注意无解的情况

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; const int maxN=201;
const int maxM=maxN*maxN;
const int inf=2147483647; int n; class Graph
{
private:
int cnt;
int Head[maxN];
int W[maxM];
int Next[maxM];
int V[maxM];
//priority_queue<Data,vector<Data>,greater<Data> > Q;
queue<int> Q;
bool inqueue[maxN];
public:
int s,t;
int Dist[maxN];
void init()
{
cnt=0;
memset(Head,-1,sizeof(Head));
memset(Next,-1,sizeof(Next));
}
void Add_Edge(int u,int v,int w)
{
cnt++;
Next[cnt]=Head[u];
Head[u]=cnt;
V[cnt]=v;
W[cnt]=w;
}
void spfa()
{
while (!Q.empty())
Q.pop();
for (int i=1;i<=n;i++)
Dist[i]=inf;
memset(inqueue,0,sizeof(inqueue));
Dist[s]=0;
Q.push(s);
inqueue[s]=1;
do
{
int u=Q.front();
Q.pop();
for (int i=Head[u];i!=-1;i=Next[i])
{
if (Dist[V[i]]>Dist[u]+W[i])
{
Dist[V[i]]=Dist[u]+W[i];
if (inqueue[V[i]]==0)
{
Q.push(V[i]);
inqueue[V[i]]=1;
}
}
}
inqueue[u]=0;
}
while (!Q.empty());
return;
}
void Outp()
{
for (int i=1;i<=n;i++)
{
for (int j=Head[i];j!=-1;j=Next[j])
{
cout<<"("<<i<<","<<V[j]<<") "<<W[j]<<endl;
}
cout<<endl;
}
}
}; Graph G; int main()
{
G.init();
cin>>n>>G.s>>G.t;
for (int i=1;i<=n;i++)
{
int T;
cin>>T;
int v;
cin>>v;
G.Add_Edge(i,v,0);
if (T<2)
continue;
for (int j=2;j<=T;j++)
{
cin>>v;
G.Add_Edge(i,v,1);
}
}
//G.Outp();
G.spfa();
if (G.Dist[G.t]==inf)
cout<<-1<<endl;
else
cout<<G.Dist[G.t]<<endl;
return 0;
}

POJ 1847 Tram (最短路径)的更多相关文章

  1. 最短路 || POJ 1847 Tram

    POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...

  2. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  3. poj 1847 Tram

    http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...

  4. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  5. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  6. (简单) POJ 1847 Tram,Dijkstra。

    Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...

  7. Floyd_Warshall POJ 1847 Tram

    题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...

  8. POJ 1847 Tram --set实现最短路SPFA

    题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...

  9. POJ 1847 Tram【Floyd】

    题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...

随机推荐

  1. ES6 箭头函数易出错细节

    箭头函数表达式的语法比函数表达式更短,并且没有自己的this,arguments,super或 new.target. 箭头函数基本语法 (参数1, 参数2, -, 参数N) => { 函数声明 ...

  2. Android AccessibilityService(辅助服务) 使用示例

    1.前言 网上关于Android辅助服务的使用方式已经非常丰富了,所以也不在乎再多我这一篇了:-D.有同学说这是重复造轮子,题主很同意,但反过来说,如果自己没有能力造出轮子,还对重复造轮子嗤之以鼻,那 ...

  3. mybatis源码-解析配置文件(三)之配置文件Configuration解析

    目录 1. 简介 1.1 系列内容 1.2 适合对象 1.3 本文内容 2. 配置文件 2.1 mysql.properties 2.2 mybatis-config.xml 3. Configura ...

  4. 1、Docker概述与安装

    1.Docker概述 原文地址:https://docs.docker-cn.com/engine/docker-overview/#docker-engine Docker是一个开发,集装,运行应用 ...

  5. fis入门-单文件编译之文件优化(optimize)

    FIS(Front-end Integrated Solution ),是百度的前端集成解决方案.最近几天在研究前端构建的东西,就顺便了解了下,果断各种高大上,可以到FIS官网围观感受一下.如果对fi ...

  6. commitizen和cz-customizable配置git commit message

    起因 团队对提交的commit message格式有约定俗称的要求,但是没有一个统一的规范,导致大家提交的commit message或多或少不太一样.因此,需要一个工具来帮助大家统一commit m ...

  7. 智能合约bug以及修改方案

    截取两篇文章:第一遍文章说的是智能合约能不能修改的问题: ETC转到ETH地址以及转币进ETH智能合约账户能不能转出来? 第0章 引言 如果ETC充值到了ETH地址上,能找回来吗?答案是不一定. ET ...

  8. Linux内核读书笔记第二周

    什么是系统调用 简单来说,系统调用就是用户程序和硬件设备之间的桥梁.用户程序在需要的时候,通过系统调用来使用硬件设备. 系统调用的存在,有以下重要的意义: 1)用户程序通过系统调用来使用硬件,而不用关 ...

  9. IIS错误提示:另一个程序正在使用此文件 进程无法访问

    在IIS管理中,启动一个配置好的网站时,提示:另一个程序正在使用此文件 进程无法访问 原因:网站绑定端口被占用 解决办法:更换绑定端口或者将占用此端口的程序关掉即可

  10. webpack 学习笔记 (一)

    webpack 作为当下前端前沿最受欢迎的打包工具,作为一个前端开发人员是很有必要去了解下它的. 题外话: npm i -D 是 npm install  --save-dev的简写,是安装模块并保存 ...