POJ1847 Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 20274 | Accepted: 7553 |
Description
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
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
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的更多相关文章
- poj1847 Tram(最短路dijkstra)
描述: Tram network in Zagreb consists of a number of intersections and rails connecting some of them. ...
- poj1847 Tram(Dijkstra || Floyd || SPFA)
题目链接 http://poj.org/problem?id=1847 题意 有n个车站,编号1~n,每个车站有k个出口,车站的出口默认是k个出口中的第一个,如果不想从默认出口出站,则需要手动选择出站 ...
- POJ1847 Tram SPFA算法变形
原题地址:http://poj.org/problem?id=1847 Tram:有轨电车 这题就是构造一个有向无权图,然后每一个点都会有一个开关,这个开关指向他的其中一个出度.当途经这个点的时候,如 ...
- POJ-1847 Tram( 最短路 )
题目链接:http://poj.org/problem?id=1847 Description Tram network in Zagreb consists of a number of inter ...
- poj1847 Tram 最短路Dijkstra
题目链接:http://poj.org/problem?id=1847 Dijkstra算法的模版应用 题意:给你N个点和起点终点,点与点有铁路,接下来的N行分别为点i的情况 第一个数字表示与该点连通 ...
- poj练习题的方法
poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...
- POJ1847:Tram(最短路)
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20116 Accepted: 7491 题目链接:http:/ ...
- N - Tram - poj1847(简单最短路)
题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个 ...
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
随机推荐
- Maven常用命令:
Maven库: http://repo2.maven.org/maven2/ Maven依赖查询: http://mvnrepository.com/ 一,Maven常用命令: 1. 创建Maven的 ...
- Python3+unitest自动化测试初探(下篇)
目录 9.用例结果校验 10.跳过用例 11.Test Discovery 12.加载用例 unittest官方文档 本篇随笔承接: Python3+unitest自动化测试初探(中篇) Python ...
- 【Java】几道让你拿offer的知识点
前言 只有光头才能变强 之前在刷博客的时候,发现一些写得比较好的博客都会默默收藏起来.最近在查阅补漏,有的知识点比较重要的,但是在之前的博客中还没有写到,于是趁着闲整理一下. 文本的知识点: Inte ...
- Docker进阶之九:Dockerfile 及 通过Dockerfile搭建lnmp
一.Dockerfile指令 指令 描述 指令 描述 FROM 构建的新镜像是基于哪个镜像 例如:FROM centos:6 COPY 拷贝文件或目录到镜像,用法同上例如:COPY ./start.s ...
- golang命令行库cobra的使用
简介 Cobra既是一个用来创建强大的现代CLI命令行的golang库,也是一个生成程序应用和命令行文件的程序.下面是Cobra使用的一个演示: Cobra提供的功能 简易的子命令行模式,如 app ...
- WCF优雅使用 KnownType标记的方法
[KnownType("DerivedTypes")] [DataContract] public abstract class TaskBase { // other class ...
- VS code 设置中文后也显示英文的问题
按f1 搜索 Configore Display Language 设置 zh-cn 关闭软件重启. 如果重启菜单等还是英文的,在商店查看已安装的插件,把中文插件重新安装一遍,然后重启软件.
- 一文搞定MySQL的事务和隔离级别
一.事务简介 事务是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成. 一个数据库事务通常包含了一个序列的对数据库的读/写操作.它的存在包含有以下两个目的: 为数据库操作序列提供 ...
- vue-router 用户登陆
有些路由页面需要用户登陆之后才能访问如(用户中心),如果用户没有登陆就访问这些页面的话就应该转换到登陆页面,登陆成功之后在进入该页面. 需要用到的知识点有:H5中的会话存储(sessionStorag ...
- Android远程桌面助手(B1309)
修改了窗口缩放的处理,支持Android Car等非常规分辨率的Android设备: 修改了获取Android端软件版本的方法,优化了APK的升级逻辑: 优化了远程输入法功能,支持利用PC端输入法快速 ...