Tram
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 20274   Accepted: 7553

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+堆优化,比较简单的一道题。

说一下思路吧:
每个节点与所有可到达节点之间连边,与初始指向节点的权值为0,与其余可到达的节点的权值为1。
然后求最短路。

#include <cstdio>
#include <queue>
using namespace std; inline int read()
{
int x=0,f=1;char c=getchar();
while (c<'0' || c>'9'){if (c=='-')f=-1;c=getchar();}
while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-48;c=getchar();}
return x*f;
} inline void print(int x)
{
if (x<0)x=-x,putchar('-');
if (x>9)print(x/10);
putchar(x%10+48);
} inline void print(int x,char c)
{
print(x);
putchar(c);
} const int INF=10000000;
const int MAXN=101;
int n,from,to;
int a[MAXN];
int cost[MAXN][MAXN]; struct dij
{
int id,dis;
bool operator < (const dij tmp) const
{
return dis>tmp.dis;
}
};
int dis[MAXN]; inline void dijkstra()
{
int u;
for (int i=1;i<=n;i++)dis[i]=INF;
dis[from]=0;
priority_queue<dij> Q;
Q.push((dij){from,0});
while (!Q.empty())
{
u=Q.top().id;Q.pop();
for (int i=1;i<=n;i++)
if (dis[u]+cost[u][i]<dis[i])
{
dis[i]=dis[u]+cost[u][i];
Q.push((dij){i,dis[i]});
}
}
} int main()
{
n=read();from=read();to=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
cost[i][j]=INF;
for (int i=1;i<=n;i++)cost[i][i]=0;
int top,k;
for (int i=1;i<=n;i++)
{
top=read();
if (!top)continue;
cost[i][read()]=0;
for (int j=2;j<=top;j++)cost[i][read()]=1;
}
dijkstra();
if (dis[to]<INF)print(dis[to],'\n');
else print(-1,'\n');
return 0;
}

POJ1847 Tram的更多相关文章

  1. poj1847 Tram(最短路dijkstra)

    描述: Tram network in Zagreb consists of a number of intersections and rails connecting some of them. ...

  2. poj1847 Tram(Dijkstra || Floyd || SPFA)

    题目链接 http://poj.org/problem?id=1847 题意 有n个车站,编号1~n,每个车站有k个出口,车站的出口默认是k个出口中的第一个,如果不想从默认出口出站,则需要手动选择出站 ...

  3. POJ1847 Tram SPFA算法变形

    原题地址:http://poj.org/problem?id=1847 Tram:有轨电车 这题就是构造一个有向无权图,然后每一个点都会有一个开关,这个开关指向他的其中一个出度.当途经这个点的时候,如 ...

  4. POJ-1847 Tram( 最短路 )

    题目链接:http://poj.org/problem?id=1847 Description Tram network in Zagreb consists of a number of inter ...

  5. poj1847 Tram 最短路Dijkstra

    题目链接:http://poj.org/problem?id=1847 Dijkstra算法的模版应用 题意:给你N个点和起点终点,点与点有铁路,接下来的N行分别为点i的情况 第一个数字表示与该点连通 ...

  6. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  7. POJ1847:Tram(最短路)

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20116   Accepted: 7491 题目链接:http:/ ...

  8. N - Tram - poj1847(简单最短路)

    题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个 ...

  9. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

随机推荐

  1. 修改VS2017新建类模板文件添加注释

    找到Class.cs文件 找到VS2017安装目录下面的Class.cs文件,一般在C盘或者D盘 我的VS2017安装在D盘,所以在D盘以下目录找到 D:\Program Files (x86)\Mi ...

  2. C#工具:MySQL忘记密码解决方法

    1.进入管理员控制台停止mysql服务:net stop mysql; 2.进入mysql的安装路径,如我的安装路径为C:\Program Files\MySQL\MySQL Server 5.5,打 ...

  3. Springboot2注解使用Mybatis动态SQL

    1.简单SQL使用 //从***数据表获取统计数据 @Select("select count(*) from issues where issue_type = #{type}" ...

  4. Snapde电子表格编写Exprtk脚本进行数据运算

    Snapde,一个专门为编辑超大型数据量CSV文件而设计的单机版电子表格软件:它运行的速度非常快,反应非常灵敏. 一.打开文件:用Snapde打开需要运算的CSV文件 二.添加行列:在编辑菜单找到设置 ...

  5. Android项目实战欢迎界面

    欢迎界面 首先同理把欢迎界面的图片导入到drawable目录下,在导入时 Android Studio 会提示如下 drawable 具体本人尚未弄明白,待理解后会重新补全本部分内容,在此本人选了第一 ...

  6. 解决Angular2 (SystemJS) XHR error (404 Not Found) loading traceur

    初学Angular2,跟着Angular2中文网学到HTTP这一节时出现了一个异常: GET http://localhost:3000/traceur 404 (Not Found) Error: ...

  7. bcrypt 安装不成功解决办法

    同一个项目,公司和家里的 node.js 的版本不同,导致项目安装依赖包时 bcrypt 安装不成功. 家里的版本为:8.11.3 公司的版本为:10.14.2 在当前项目中执行完下面两个命令后,报错 ...

  8. SQL SERVER 2012 AlwaysOn - 操作系统层面 01

    搭建 AlwaysOn 是件非常繁琐的工作,需要从两方面考虑,操作系统层面和数据库层面,AlwaysOn 非常依赖于操作系统,域控,群集,节点等概念: DBA 不但要熟悉数据库也要熟悉操作系统的一些概 ...

  9. 基于FPM制作nginx RPM包

    目录 环境 配置 FPM安装 环境 系统 其它 CentOS 7.5 需提前配置好epel 配置 [root@localhost ~]# yum clean all && yum ma ...

  10. ASP.NETMVC 分页

    <div class="text-center">    <span style="display:inline-block;  position:re ...