POJ 1847 Tram (最短路)
Tram
题目链接:
http://acm.hust.edu.cn/vjudge/contest/122685#problem/N
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
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
Hint
##题意:
求图中#A到#B的最短路.
##题解:
裸的最短路,数据非常小,随便求就好.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n,s,t;
int dis[maxn][maxn];
void init() {
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dis[i][j] = (i==j? 0:inf);
}
void floyd() {
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(dis[i][j] > dis[i][k]+dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
int main(void)
{
//IN;
while(scanf("%d %d %d", &n,&s,&t) != EOF)
{
init();
for(int i=1; i<=n; i++) {
int m; scanf("%d", &m);
for(int j=1; j<=m; j++) {
int x; scanf("%d", &x);
dis[i][x] = j==1? 0:1;
}
}
floyd();
if(dis[s][t] == inf) printf("-1\n");
else printf("%d\n", dis[s][t]);
}
return 0;
}
POJ 1847 Tram (最短路)的更多相关文章
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
- poj 1847 Tram
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- POJ 1847 Tram【Floyd】
题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...
- Floyd_Warshall POJ 1847 Tram
题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...
随机推荐
- Eclipse中查看JDK源码设置
设置方法如下: 1.路径 window-> Preferences -> Java -> Installed JRES 2.此时"Installed JRES"右 ...
- Awesome-awesome-awesome
Awesome-awesome-awesome A curated list of curated lists of awesome lists. awesome-awesomes @sindreso ...
- cd /d %~dp0
cd /d %~dp0 注册服务的bat要用到,普通的执行没关系,但是用“管理员角色”执行,默认的起始目录会到 c:\windows\system 下,用上面一句可以回到当前执行bat的目录
- 强势解决:windows 不能在本地计算机中起动Tomcat参考特定错误代码1
Tomcat添加系统服务:service.bat install 启动本服务的时候却提示“windows 不能在本地计算机中起动 Apache Tomcat参考特定错误代码1,若不是Microsoft ...
- <pages validateRequest="false"/>在.net4.0中无效的问题
再web.config中设置<pages validateRequest="false"/>在.net4.0中无效的问题 解决方案: <system.web> ...
- tkprof 解释
使用 tkprof 工具 tkprof orcl_ora_3048_安庆怀宁.trc 安徽安庆怀宁.txt sys=no aggregate=yes sys=no waits=yes sort=fc ...
- 极光推送使用实例(二) Android客户端
上一篇简单介绍了极光推送在Java服务端的实现,如果感兴趣的可以看一下极光推送使用实例(一)JAVA服务端.这篇文章介绍下极光推送在Android客户端的实现. JPush Android SDK 是 ...
- JavaScript学习笔记(备忘录)
===运算符 判断数值和类型是否相等.如: console.log('s'==='s') //输出trueconsole.log('1'===1) //输出false
- PHP最佳实践(译)
原文: PHP Best Practices-A short, practical guide for common and confusing PHP tasks 译者:youngsterxyf 最 ...
- Delphi Unable to invoke Code Completion due to errors in source code
这时因为在.pas文件中存在delphi无法识别的编码,也就是说.pas文件中的字符并非是纯粹的可由文本文件编辑器所能识别的编码.所以,delphi就不可能有效地解释这些编码.因而就出现了自动代码提示 ...