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. 20155211 Exp4 恶意代码分析

    20155211 Exp4 恶意代码分析 实践目标 1 监控你自己系统的运行状态,看有没有可疑的程序在运行. 2 是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或 ...

  2. EZ 2017 12 17初二初三第一次膜你赛

    以后平时练习还是写一写吧. (题目搞来搞去太烦了,直接PDF存起来) T1 水题(???),主要是数据水,正解是设一个阙值,然而根本没人打.(暴力出奇迹) CODE #include<cstdi ...

  3. python 回溯法 子集树模板 系列 —— 8、图的遍历

    问题 一个图: A --> B A --> C B --> C B --> D B --> E C --> A C --> D D --> C E -- ...

  4. WordPress留言本插件推荐

    WordPress不借助于任何插件也可以做个留言本,那就是建个 Page, 直接使用它的评论功能即可,而且给评论加上 Ajax 功能.WYSIWYG.引用.回复.留言分页等功能也可以做的很漂亮.但对于 ...

  5. 几个不常用的 Web API

    1. 设备震动 vibrate Navigator.vibrate() 方法使设备(有震动硬件)产生有频率的震动.若设备不支持震动,该方法将无效.若某震动方式已经在进行中(当该方法调用时),则前一个震 ...

  6. django请求的生命周期

    1. 概述 首先我们知道HTTP请求及服务端响应中传输的所有数据都是字符串. 在Django中,当我们访问一个的url时,会通过路由匹配进入相应的html网页中. Django的请求生命周期是指当用户 ...

  7. NAND Flash底层原理,SLC MLC TLC比较

    NAND-Flash 的存储原理 固态硬盘最小单元的基本架构如下: 我们知道计算机中所有的信息储存最终都必须回归到 0与1,原则上,只要存储单元能提供两种或两种以上可供辨识的状态,便可以拿来纪录数据. ...

  8. 在Microsoft Dynamic 365/2016环境使用LinqPad查询数据(不使用linqpad Microsoft Dynamic 365 Driver)

    在Microsoft Dynamic 365/2016环境使用LinqPad查询数据 老规矩,先上效果图: 实体集合: 实体属性: 属性值:  查询出的结果可以导出的格式: 操作步骤: 1.下载Lin ...

  9. Flink standalone模式作业执行流程

    宏观流程如下图: client端 生成StreamGraph env.addSource(new SocketTextStreamFunction(...)) .flatMap(new FlatMap ...

  10. Delphi中 弹出框的用法

    Delphi中的提示框有 Application.MessageBox  ShowMessage messagedlg 个人认为 相对来说 Application.MessageBox 更加灵活 也相 ...