POJ 1847:Tram
2015-08-06 10:33
323 查看
Tram
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 <= 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
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
Sample Output
这题的题意是给了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口,走的路程想要转的次数时最少的,问最少转的值。
每一行的第2个数字,其距离为0。其余的距离设置为1。
代码:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11771 | Accepted: 4301 |
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 <= 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
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
这题的题意是给了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口,走的路程想要转的次数时最少的,问最少转的值。
每一行的第2个数字,其距离为0。其余的距离设置为1。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #pragma warning(disable:4996) using namespace std; int num; int dis[1005][1005]; void init() { int i,j; for(i=1;i<=num;i++) { for(j=1;j<=num;j++) { if(i==j) { dis[i][j]=0; } else { dis[i][j]=1005; } } } } int main() { int i,j,k,i_num,x,y; cin>>num>>x>>y; init(); for(i=1;i<=num;i++) { cin>>i_num; int h; for(j=1;j<=i_num;j++) { cin>>h; if(j==1)//第一个数字代表原来的方向,不需要转 dis[i][h]=0; else //之后代表要转 dis[i][h]=1; } } for(k=1;k<=num;k++) { for(i=1;i<=num;i++) { for(j=1;j<=num;j++) { if(dis[i][k]+dis[k][j]<dis[i][j]) { dis[i][j]=dis[i][k]+dis[k][j]; } } } } if(dis[x][y]>=1000) cout<<-1<<endl; else cout<<dis[x][y]<<endl; return 0; }
相关文章推荐
- MyEclipse 快捷键
- opencv GPU HoughLines直线
- iOS设计模式之观察者模式
- POJ 1847:Tram
- win2008清理C盘
- 深入java单例模式
- 图像显示
- iTunes Connect 开发者指南 (iTunes Connect Developer Guide)
- 极客学院使用体会
- 南邮 OJ 1659 牛顿苹果
- java图片截取上传
- cannot determine the location of the vs common tools folder
- 前端开发中常用的几种图片格式及其使用规范
- Dynamics AX R2 通过数据源保存记录时触发的方法
- 使用jquery排重并排序
- 九度oj 1450
- javascript闭包理解
- 水_CF_567A
- python自动化,读取文件夹内所有文件名字,并做简单的后缀名匹配
- 开发rtmp直播流测试地址