poj 1847 Tram
Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8743 | Accepted: 3184 |
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.
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.
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
这题只要读懂题用flody就可以了
输入描述:给你n个路口数,以及你的起点和终点
然后是n行
每一行的第一个数是该路口可以到达的路口数k 然后是k个路口,在这k个路口中第一个路口是可以直达的也就是不用按按钮的,要想到达别的路口就得按按钮了
思路:K个路口的第一个路口记为0,其他的记为1,然后直接用flody的行了。
#include<stdio.h> #include<string.h> int dis[105][105]; #define INF 0x3f3f3f3f int Min(int a,int b) { return a<b?a:b; } int main() { int n,m,a,b,i,j,k,x,y; while(scanf("%d %d %d",&n,&a,&b)!=EOF) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) dis[i][j]=(i==j)?0:INF; for(i=1;i<=n;i++) { scanf("%d",&m); for(j=1;j<=1;j++) { scanf("%d",&x); dis[i][x]=0; } for(j=2;j<=m;j++) { scanf("%d",&x); dis[i][x]=1; } } for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) dis[i][j]=Min(dis[i][j],dis[i][k]+dis[k][j]); if(dis[a][b]<INF) printf("%d\n",dis[a][b]); else printf("-1\n"); } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用