POJ1847 Tram(最短路径)

xiaoxiao2021-02-27  339

Tram Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 15377 Accepted: 5653

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

3 2 1 2 2 3 2 3 1 2 1 2

Sample Output

0

Source

Croatia OI 2002 Regional - Juniors

思路:先说下题意,在变轨的时候可能会有多条轨道,但除了第一条轨道外其他的轨道都要手动打开,代价为1,求从A到B花费的最小代价。在输入的时候,可以对输入的第一条轨道赋值为0,其余赋值为1求解即可。

dijkstra:

#include <cstdio> #include <cstring> #define INF 0x3f3f3f3f using namespace std; int n,a,b,map[105][105],vis[105],dis[105]; void dijkstra(){ for(int i = 1; i <= n; i ++){ dis[i] = map[a][i]; } vis[a] = 1; for(int i = 1; i <= n; i ++){ if(i != a){ int k=a,Min = INF; for(int j = 1; j <= n; j ++){ if(!vis[j] && dis[j] < Min){ Min = dis[j]; k = j; } } vis[k] = 1; for(int j = 1; j <= n; j ++){ if(!vis[j] && dis[j] > dis[k] + map[k][j]) dis[j] = dis[k] + map[k][j]; } } } } int main(){ while(~scanf("%d%d%d",&n,&a,&b)){ int x,y,k=1; for(int i = 1; i <= n; i ++){ for(int j = 1; j <= n; j ++){ if(i == j) map[i][j] = 0; else map[i][j] = INF; } vis[i] = 0; } for(int i = 0; i < n; i ++){ scanf("%d",&x); for(int j = 0; j < x; j ++){ scanf("%d",&y); if(j) map[k][y] = 1; else map[k][y] = 0; } k ++; } dijkstra(); if(dis[b] >= INF) printf("-1\n"); else printf("%d\n",dis[b]); } return 0; }

SPFA:

#include <cstdio> #include <queue> #include <cstring> #define INF 0x3f3f3f3f using namespace std; int n,a,b,head[105],cnt,dis[105],vis[105]; struct node{ int val,to,next; }edge[105]; void add(int u, int v, int w){ edge[cnt].val = w; edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt ++; } void spfa(){ queue<int>q; for(int i = 1; i <= n; i ++){ dis[i] = INF; vis[i] = 0; } dis[a] = 0; q.push(a); vis[a] = 1; while(!q.empty()){ int v = q.front(); q.pop(); vis[v] = 0; for(int i = head[v]; i != -1; i = edge[i].next){ int c = edge[i].to; if(dis[c] > dis[v] + edge[i].val){ dis[c] = dis[v] + edge[i].val; if(!vis[c]){ vis[c] = 1; q.push(c); } } } } } int main(){ while(~scanf("%d%d%d",&n,&a,&b)){ memset(head,-1,sizeof(head)); cnt = 1; for(int i = 0; i < n; i ++){ int x,y; scanf("%d",&x); for(int j = 0; j < x; j ++){ scanf("%d",&y); int val=1; if(!j) val = 0; add(i+1,y,val); } } spfa(); if(dis[b] >= INF) printf("-1\n"); else printf("%d\n",dis[b]); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-4323.html

最新回复(0)