poj1847Tram 最短路 floyd dijkstra
来源:互联网 发布:淘宝借贷利息 编辑:程序博客网 时间:2024/06/04 19:43
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.
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.
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".
3 2 12 2 32 3 12 1 2
0
题意: 第一行n个点,求a到b的转换次数,若没有路线输出-1.
接下来n行,第i行第一个数字表示第i个点可以转换点的个数t,接下来t个数,第一个数(ai)表示从i到ai自动转换为0,其他为手动转换为1
最近脑子有问题,经常看不懂题,崩溃。。。
1.floyd
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int M=110;const int inf=0x3f3f3f3f;int cost[M][M];int n,a,b;int main(){ int t,lu,i,j; while(~scanf("%d%d%d",&n,&a,&b)) { memset(cost,inf,sizeof(cost)); for( i=1;i<=n;i++) cost[i][i]=0; for(i=1;i<=n;i++) { scanf("%d",&t); for(j=1;j<=t;j++) { scanf("%d",&lu); if(j==1) cost[i][lu]=0; else cost[i][lu]=1; } } for(int k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]); if(cost[a][b]==inf) printf("-1\n"); else printf("%d\n",cost[a][b]); } return 0;}
2. dijkstra
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int M=110;const int inf=0x3f3f3f3f;int cost[M][M];int n,a,b;int vis[M];int dis[M];void dijstra(int st){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) dis[i]=cost[st][i]; vis[st]=1; for(int i=2;i<=n;i++) { int mini=inf,k; for(int j=1;j<=n;j++) { if(!vis[j]&&mini>dis[j]) { mini=dis[j]; k=j; } } vis[k]=1; for(int u=1;u<=n;u++) { if(!vis[u]&&dis[u]>dis[k]+cost[k][u]) dis[u]=dis[k]+cost[k][u]; } }}int main(){ int t,lu,i,j; while(~scanf("%d%d%d",&n,&a,&b)) { for( i=1;i<=n;i++) for(j=1;j<=n;j++) { if(i==j) cost[i][j]=0; else cost[i][j]=inf; } for(i=1;i<=n;i++) { scanf("%d",&t); for(j=1;j<=t;j++) { scanf("%d",&lu); if(j==1) cost[i][lu]=0; else cost[i][lu]=1; } } dijstra(a); if(dis[b]==inf) printf("-1\n"); else printf("%d\n",dis[b]); } return 0;}
阅读全文
0 0
- poj1847Tram 最短路 floyd dijkstra
- 最短路dijkstra,floyd
- 最短路Floyd、Dijkstra
- poj1847Tram (最短路)
- hdu2544最短路(floyd)(dijkstra)
- 最短路 spfa, dijkstra, Floyd
- 最短路模版 【dijkstra】【floyd】
- 最短路【dijkstra】【floyd 】【spfa】
- hdu 2544 最短路 (dijkstra/floyd/spfa)
- HDU 2544 最短路 dijkstra floyd
- [dijkstra/SPFA/floyd]HDU 2544最短路
- hdu2544 最短路(Floyd,Dijkstra算法)
- 【最短路三算法】Floyd,Dijkstra,SPFA.
- 最短路 Dijkstra 和 Floyd 算法
- POJ2253 Frogger -DIJKSTRA || FLOYD最短路练习
- 最短路:Dijkstra算法和Floyd算法
- 最短路模板 Dijkstra+Floyd+SPFA
- HDU2544---最短路(dijkstra&&floyd&&spfa)
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-前言与目录(持续更新中...)
- 算法设计与分析
- React Native原生模块的基本使用
- 不相交集ADT-概述
- window ,linux yaf创建目录
- poj1847Tram 最短路 floyd dijkstra
- 田忌赛马
- Ionic+Angularjs+Cordova开发跨平台混合式移动应用
- 前端基本功—js第三天
- Android 代码优化工具FindBugs
- 如何让机器拥有人类的智慧?| 论文访谈间 #09
- cinder基本使用
- HDU 6154 CaoHaha's staff(找规律)
- 记录一次ELK集群优化