POJ_1847_Tram
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 11159 | Accepted: 4089 |
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
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
0
Source
#include<iostream>
#include<cstdio>
using namespace std;
#define N 1000000
int map[][];
int main()
{
int n,sta,end,m,a;
int i,j; scanf("%d%d%d",&n,&sta,&end);
for(i=; i<=n; i++)
for(j=; j<=n; j++)
map[i][j]=N;
for(i=; i<=n; i++)
{
scanf("%d",&m);
for(j=; j<=m; j++)
{
scanf("%d",&a);
if(j==)
map[i][a]=;
else
map[i][a]=;
}
} int k;
for(k=; k<=n; k++) //floyd算法
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(map[i][k]+map[k][j]<map[i][j])
map[i][j]=map[i][k]+map[k][j]; if(map[sta][end]<N)
cout<<map[sta][end]<<endl;
else
cout<<-<<endl;
return ;
}
POJ_1847_Tram的更多相关文章
随机推荐
- 爸爸和儿子的故事带你理解java线程
今天回想线程方面的知识,发现一个非常有意思的小程序.是用来说明多线程的以下贴出来分享下,对刚開始学习的人理解线程有非常大的帮助 爸爸和儿子的故事 <span style="font-f ...
- Linux系统下ssh登陆很慢的解决办法
很多的Linux用户发现连接上Linux服务器在输入用户名之后还要再等一下才能输入密码,时间过长了,现在小白与大家分享一下如何解决ssh登陆问题的问题,希望对您有所帮助. 1.我们平时登陆Linux服 ...
- UNION(并集)集合运算
在集合论中,两个集合(记为集合A和B)的并集是一个包含集合A和B中所有元素的集合.换句话说,如果一个元素属于任何一个输入集合,那么它也属于结果集. 在T-SQL中,UNION 集合运算可以将两个输入查 ...
- 单一责任原则(SRP)
1.就一个类而言,应该仅有一个引起它变化的原因. 2.在SRP中定义职责为:“变化的原因”. 如果你想到多个动机去改变这个类,那这个类就有多个职责
- Codechef Racing Horses题解
找一个数组中两数之间最小的不同值. 思路: 1 排序 2 后前相减,比較得到最小不同值 三个数甚至很多其它数之间的不同值都是这么求了,时间效率都是O(nlgn) -- 排序使用的时间 原题: http ...
- MySQL-PREPARE语句
MySQL-PREPARE语句 功能介绍: MySQL准备语句用法 为了使用MySQL准备语句,您需要使用其他三个MySQL语句如下: PREPARE - 准备执行的声明. EXECUTE - 执行由 ...
- ramfs、rootfs和initramfs【转】
ramfs, rootfs and initramfs October 17, 2005 Rob Landley <rob@landley.net> =================== ...
- ssh服务常见问题及其解决办法
1 统一解决办法 执行sshd -t,这样就可以指出是哪里出问题了. 所有的服务都应该有这个测试选项,否则出错了都不知道在哪里出的问题. 2 root用户登录,密码是对的,但是报“Permission ...
- 167. Two Sum II - Input array is sorted (二分ortwo-pointer)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- html5 弹性布局
html5 弹性布局 一.移动开发常用技巧 Viewport基本知识 设置布局Viewport的各种信息1.width=device-width: 设置Viewport视口宽度等于设备宽度2.init ...