poj1847 Tram(最短路dijkstra)
描述:
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
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
Sample Input
3 2 1
2 2 3
2 3 1
2 1 2
Sample Output
题意:
有n个点,并且给了起点和终点ab,接下来的n行的第一个数k表示与第i个点相连的点数,每一行接下来有k个数,表示与i点相连接的点,并且在这k个点中,第一个点可直接到达,及路径长度为0,其他的点需要改一次扳手,路径长度为1,用dijkstra即可解决。
代码:
#include <iostream>
#include <stdio.h> using namespace std;
#define inf 100100100
bool vis[];
int n,a,b;
int map[][];
int d[]; void dijkstra()
{
int i,j,v,f;
for(i=;i<=n;i++)
{
vis[i]=;
d[i]=map[a][i];
}
vis[a]=;
d[a]=;
for(i=;i<n;i++)
{
f=inf;v=a;
for(j=;j<=n;j++)
{
if(d[j]<inf&&!vis[j]&&d[j]<f)
{
f=d[j];
v=j;
}
}
if(f>inf) break;
vis[v]=;
for(j=;j<=n;j++)
if(!vis[j]&&map[v][j]<inf&&d[v]+map[v][j]<d[j])
d[j]=d[v]+map[v][j];
}
} int main()
{
int k,m;
scanf("%d%d%d",&n,&a,&b);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i==j) map[i][j]=;
else map[i][j]=inf;
}
for(int i=;i<=n;i++)
{
scanf("%d",&k);
for(int j=;j<k;j++)
{
scanf("%d",&m);
if(j==) map[i][m]=;
else map[i][m]=;
}
}
dijkstra();
if(d[b]>=inf) cout<<"-1"<<endl;
else
cout<<d[b]<<endl;
return ;
}
poj1847 Tram(最短路dijkstra)的更多相关文章
- poj1847 Tram 最短路Dijkstra
题目链接:http://poj.org/problem?id=1847 Dijkstra算法的模版应用 题意:给你N个点和起点终点,点与点有铁路,接下来的N行分别为点i的情况 第一个数字表示与该点连通 ...
- POJ-1847 Tram( 最短路 )
题目链接:http://poj.org/problem?id=1847 Description Tram network in Zagreb consists of a number of inter ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
- 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法
图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...
- 单源最短路dijkstra算法&&优化史
一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...
- HUD.2544 最短路 (Dijkstra)
HUD.2544 最短路 (Dijkstra) 题意分析 1表示起点,n表示起点(或者颠倒过来也可以) 建立无向图 从n或者1跑dij即可. 代码总览 #include <bits/stdc++ ...
- 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)
layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...
- 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)
layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- 最短路Dijkstra算法的一些扩展问题
最短路Dijkstra算法的一些扩展问题 很早以前写过关于A*求k短路的文章,那时候还不明白为什么还可以把所有点重复的放入堆中,只知道那样求出来的就是对的.知其然不知其所以然是件容易引发伤痛的 ...
随机推荐
- XPath、CSS 选择器 -学习地址
http://www.w3school.com.cn/cssref/css_selectors.asp http://www.w3school.com.cn/xpath/xpath_syntax.as ...
- Spring-webflow基础讲解
什么是webflow: Spring Web Flow构建于Spring MVC之上,允许实现Web应用程序的“流程”.流程封装了一系列步骤,指导用户执行某些业务任务.它跨越多个HTTP请求,具有状态 ...
- MySQL数据库存储引擎
这里主要介绍最常用的两种存储引擎. 1.InnoDB InnoDB是一个事务型的存储引擎,有行级锁定和外键约束.Innodb引擎提供了对数据库ACID事务的支持,并且实现了SQL标准的四种隔离级别,关 ...
- springdata 查询思路:基本的单表查询方法(id,sort) ---->较复杂的单表查询(注解方式,原生sql)--->实现继承类---->复杂的多表联合查询 onetomany
springdata 查询思路:基本的单表查询方法(id,sort) ---->较复杂的单表查询(注解方式,原生sql)--->实现继承类---->复杂的多表联合查询 onetoma ...
- 自动化测试框架【linux版】:JMeter + Ant + Jenkins
前提条件:linux安装了jmeter.ant.jenkins 安装方法参考汇总目录中对应的博文 截图看不清的,可以调大浏览器倍数看 jenkins驱动ant执行,ant驱动jmeter执行 ant调 ...
- Vue-route实现原理
1.原理图如下所示 2.说明 1.安装插件的时候监听hashchange事件,监视_route 2.处理router-link 获取到path关联组件 3.等待hashchange触发,匹配route ...
- Python学子之如何退出python 命令行
python命令行是新手学习python过程中必须要学的一个工具,下面我们来看一下怎么退出python命令行. 工具/原料 python2.7 方法/步骤 1.我们这里使用的是python3.6版本, ...
- Java IO系列之四:NIO通信模型
分布式rpc框架有很多,比如dubbo,netty,还有很多其他的产品.但他们大部分都是基于nio的, nio是非阻塞的io,那么它的内部机制是怎么实现的呢. 1.由一个专门的线程处理所有IO事件,并 ...
- flask websocker
WebSocket 是一种网络通信协议.RFC6455 定义了它的通信标准. HTTP 协议是一种无状态的.无连接的.单向的应用层协议.它采用了请求/响应模型.通信请求只能由客户端发起,服务端对请求做 ...
- OAuth2
OAuth2: 适合To C的应用场景, 比如我们开发一个app, 可以借用微信/微博用户认证开放接口, 达到免注册登陆, 企业内部系统没有必要引入. OAuth2的步骤较多, 角色也较多, 涉及到a ...