poj 1847 Tram(最短路径)
2014-12-08 19:52
459 查看
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个点 接下来的N行分别为点i的情况:第一个数字k表示与该点连通的点的个 数,接下来输入k个数,表示与点i相连的点的编号,第一个所连的点为不用 手动改而直接通过,其余的点通过的话要改一次,求从A点到B点改扳手的最小次数。
最短路问题:
(1)Dijkstr算法
(2)spfa算法
(3)floyd算法
(4)dfs
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个点 接下来的N行分别为点i的情况:第一个数字k表示与该点连通的点的个 数,接下来输入k个数,表示与点i相连的点的编号,第一个所连的点为不用 手动改而直接通过,其余的点通过的话要改一次,求从A点到B点改扳手的最小次数。
最短路问题:
(1)Dijkstr算法
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int map[101][101]; int N,A,B; int Dijkstr(int u,int v) { bool vis[101]; int dist[101]; for (int i=1; i<=N; i++) { dist[i]=map[u][i]; vis[i]=false; } vis[u]=true; dist[u]=0; for (int i=1; i<=N; i++) { int k=-1; int Min=1000000; for (int j=1; j<=N; j++) { if(dist[j]<Min && !vis[j]) { Min=dist[j]; k=j; } } vis[k]=true; for (int j=1; j<=N; j++) { if(!vis[j] && Min+map[k][j]<dist[j]) { dist[j]=Min+map[k][j]; } } } return dist[v]==1000000? -1:dist[v]; } int main() { int k; while (scanf("%d%d%d",&N,&A,&B)!=EOF) { for (int i=0; i<=N; i++) { for (int j=0; j<=N; j++) { map[i][j]=1000000; } } for (int i=1; i<=N; i++) { int n; scanf("%d",&k); if(k) { scanf("%d",&n); map[i] =0; } for (int j=1; j<k; j++) { scanf("%d",&n); map[i] =1; } } printf("%d\n",Dijkstr(A,B)); } return 0; }
(2)spfa算法
#include <iostream> #include <queue> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int N,A,B,cnt; struct node { int u,v,w; int next; }Edge[10001]; int head[10001]; void add(int u,int v,int w) { Edge[cnt].u=u; Edge[cnt].w=w; Edge[cnt].v=v; Edge[cnt].next=head[u]; head[u]=cnt++; } int spfa(int u,int m) { bool vis[101]={0}; int dist[101]; priority_queue<int,vector<int>,less<int> >q; for (int i=1; i<=N; i++) { dist[i]=100000; } dist[u]=0; vis[u]=true; q.push(u); while (!q.empty()) { int u=q.top(); q.pop(); vis[u]=false; for (int i=head[u]; i!=-1; i=Edge[i].next) { int v=Edge[i].v; if(dist[v]>dist[u]+Edge[i].w) { dist[v]=dist[u]+Edge[i].w; if(!vis[v]) { q.push(v); vis[v]=true; } } } } return dist[m]==100000?-1:dist[m]; } int main() { int k; while (scanf("%d%d%d",&N,&A,&B)!=EOF) { cnt=0; memset(head, -1, sizeof(head)); for (int i=1; i<=N; i++) { int n; scanf("%d",&k); if(k) { scanf("%d",&n); add(i,n,0); } for (int j=1; j<k; j++) { scanf("%d",&n); add(i,n,1); } } printf("%d\n",spfa(A,B)); } return 0; }
(3)floyd算法
#include <iostream> #include <stdio.h> #include <vector> using namespace std; int map[101][101]; int N,A,B; int main() { int k; while (scanf("%d%d%d",&N,&A,&B)!=EOF) { for (int i=0; i<=N; i++) { for (int j=0; j<=N; j++) { map[i][j]=1000000; } } for (int i=1; i<=N; i++) { int n; scanf("%d",&k); if(k) { scanf("%d",&n); map[i] =0; } for (int j=1; j<k; j++) { scanf("%d",&n); map[i] =1; } } for (int k=1; k<=N; k++) { for (int i=1; i<=N; i++) { for (int j=1; j<=N; j++) { if(map[i][k]+map[k][j]<map[i][j]) { map[i][j]=map[i][k]+map[k][j]; } } } } printf("%d\n",map[A][B]==1000000?-1:map[A][B]); } return 0; }
(4)dfs
#include <iostream> #include <queue> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int N,A,B,cnt; struct node { int v,w; int next; }Edge[10001]; int head[101]; void add(int u,int v,int w) { Edge[cnt].w=w; Edge[cnt].v=v; Edge[cnt].next=head[u]; head[u]=cnt++; } bool vis[101]; int Min; void dfs(int a,int sum) { if(sum>=Min )//剪枝 { return; } if(a==B) { if(Min>sum) { Min=sum; } vis[a]=false; return; } for (int i=head[a]; i!=-1; i=Edge[i].next) { if(!vis[Edge[i].v]) { vis[a]=true; dfs(Edge[i].v, sum+Edge[i].w); } } vis[a]=false; } int main() { int k; while (scanf("%d%d%d",&N,&A,&B)!=EOF) { cnt=0; Min=100000; memset(head, -1, sizeof(head)); memset(vis, false, sizeof(vis)); for (int i=1; i<=N; i++) { int n; scanf("%d",&k); if(k) { scanf("%d",&n); add(i,n,0); } for (int j=1; j<k; j++) { scanf("%d",&n); add(i,n,1); } } vis[A]=true; dfs(A, 0); if(Min==100000) { Min=-1; } printf("%d\n",Min); } return 0; }
相关文章推荐
- POJ_1847_Tram(单源最短路径的三种算法实现)
- POJ 1847 Tram(Dijkstra单源有向图最短路径算法)
- poj1847 - Tram (最短路径问题) (Dijkstra)
- POJ 1847 Tram (简单最短路径dijkstra-floyd-spfa)
- POJ 1847 Tram (最短路径)
- POJ1847 floyd求最短路径
- poj 1847 最短路径基础题目
- poj 1847 最短路径的dijkstra算法
- POJ 1847 最短路径 垃圾水题可是坑爹多case问题初始化的锅
- POJ-3984迷宫问题(典型BFS找最短路径)
- POJ 2455 Secret Milking Machine 求最长路径最短边(二分最大流)
- Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)
- POJ 1545 / ZOJ 1333 Galactic Import (bfs || 最短路径)
- POJ 2112:二分搜索 + 二分图多重匹配 + 最短路径
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
- POJ 1125 Stockbroker Grapevine(最短路径Floyd算法)
- POJ - 2139 Six Degrees of Cowvin Bacon(图论/无权最短路径BFS)
- 最短路径__Subway( Poj 2502 )
- poj 1847 Tram
- poj 1062 昂贵的聘礼 (最短路径)