Tram---poj1847(简单最短路)
题目链接:http://poj.org/problem?id=1847
题意:给了N个交叉口,每个交叉口有自己能转到的交叉口。
注意这里:First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection.
即每一行的第二个数字代表该交叉口默认的通向,是不需要手动转的,剩下的交叉口想去的话都需要手动转一次。
现在想要从A口走到B口,走的路程想要转的次数时最少的,问最少转的值。
建图求最短路即可
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 515 using namespace std; int G[N][N], n; void Floyd()
{
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
G[i][j] = min(G[i][j], G[i][k]+G[k][j]);
}
} int main()
{
int a, b, num, k;
while(scanf("%d %d %d", &n, &a, &b) != EOF)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
G[i][j] = INF;
G[i][i] = ;
} for(int i=; i<=n; i++)
{
scanf("%d", &k);
for(int j=; j<=k; j++)
{
scanf("%d", &num);
if(j == ) G[i][num] = ;
else G[i][num] = ;
}
}
Floyd();
if(G[a][b] == INF)puts("-1");
else printf("%d\n", G[a][b]);
}
return ;
}
Tram---poj1847(简单最短路)的更多相关文章
- N - Tram - poj1847(简单最短路)
题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个 ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- hdu2112(HDU Today 简单最短路)
Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD ...
- Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)
problem Bessie is out in the field and wants to get back to the barn to get as much sleep as possibl ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
- hdu2722 简单最短路,处理好输入就行
题意: 从左上角走到右下角,有的最短时间,每段路径的长度都是2520,每段上都有自己的限制速度,方向. 思路: 直接写就行了,就是个最短路,权值是2520/限制,输入的时候细心点 ...
- HDU 2544(简单最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=2544 /* 使用pair代替结构 */ #include <iostream> #include & ...
- HDU 1874(简单最短路) (大优化)
优先队列那里用greater会报错 http://acm.hdu.edu.cn/showproblem.php?pid=1874 /* 使用pair代替结构 */ #include <iostr ...
- LightOJ 1321 - Sending Packets 简单最短路+期望
http://www.lightoj.com/volume_showproblem.php?problem=1321 题意:每条边都有概率无法经过,但可以重新尝试,现给出成功率,传输次数和传输时间,求 ...
随机推荐
- POJ 3691 (AC自动机+状态压缩DP)
题目链接: http://poj.org/problem?id=3691 题目大意:给定N个致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题 ...
- 基于淘宝开源Tair分布式KV存储引擎的整合部署
一.前言 Tair支撑了淘宝几乎所有系统的缓存信息(Tair = Taobao Pair,Pair即Key-Value键值对),内置了三个存储引擎:mdb(默认,类似于Memcache).rdb(类似 ...
- 如何使用数据库保存session的方法简介
使用数据库保存session的方法 php的session默认是以文件方式保存在服务器端,并且在客户端使用cookie保存变量,这就会出现一个问题,当一个用户由于某种安全原因关闭了浏览器的cookie ...
- 通过IP的方式建立PSSession
Import-Module PoshWSUS Connect-PoshWSUSServer -WsusServer cnhzsrv09 Get-PoshWSUSClient | Select-Obje ...
- 关于Message目录的设定
Yii2 默认了两个message 目录,一个是 yii 和app ,所以如果想自定义message目录,要在自己配置文件中覆盖 i18n的设定,请注意,不能使用 *,要使用 app,才能生效
- C++四种强转
C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. const_cast,字面上理解就是去const属性. static_cast,命名上理解是静态类型转换.如int转换成char. ...
- linphone3.4.0代码分析
主要类型定义: 1.LinphoneCoreVTable /** * This structure holds all callbacks that the application should im ...
- Javascript 笔记与总结(2-7)对象
html: <h1>找对象</h1> <div id="div1"> <p>p1</p> <p>p2< ...
- centos7 安装及配置
第一步 下载centoshttps://www.centos.org/download/CentOS-7.0-1406-x86_64-DVD.iso:这个镜像(DVD image)包括了那些可以用安装 ...
- Configuration.ConfigurationSettings.AppSettings已过时
1.在项目中引用System.Configuration.dll,在需要的页面加上using System.Configuration; 2.把ConfigurationSettings.AppSet ...