URAL 1434 Buses in Vasyuki (双静态邻接链表+BFS)

#include<stdio.h>

#define MAX_ROUTES 1000
#define MAX_STOPS 100000
#define MAX_NODES 200000

typedef struct StopInRoute{
	int stop;
	int next;
}StopInRoute;
StopInRoute stopArray[MAX_NODES + 1];//used as adjacent list
int routeHead[MAX_ROUTES + 1];
int stopNum;

typedef struct RouteByStop{
	int route;
	int next;
}RouteByStop;
RouteByStop routeArray[MAX_NODES + 1];//used as adjacent list
int stopHead[MAX_STOPS + 1];
int routeNum;

int queue[MAX_STOPS + 1];
int head;
int tail;

int father[MAX_STOPS + 1];
int routeVisited[MAX_ROUTES + 1];

int stack[MAX_STOPS+1];
int top;

int main(){

	int numOfRoutes, numOfStops;
	scanf("%d %d", &numOfRoutes, &numOfStops);
	int indexOfRoute, indexOfStop;
	for (indexOfRoute = 1; indexOfRoute <= numOfRoutes; indexOfRoute++){
		int numOfStopsInTheRoute;
		scanf("%d", &numOfStopsInTheRoute);
		for (indexOfStop = 1; indexOfStop <= numOfStopsInTheRoute; indexOfStop++){
			int stop;
			scanf("%d", &stop);

			stopNum++;
			stopArray[stopNum].stop = stop;
			stopArray[stopNum].next = routeHead[indexOfRoute];
			routeHead[indexOfRoute] = stopNum;

			routeNum++;
			routeArray[routeNum].route = indexOfRoute;
			routeArray[routeNum].next = stopHead[stop];
			stopHead[stop] = routeNum;
		}
	}
	int startStop, endStop;
	scanf("%d %d", &startStop, &endStop);

	queue[tail++] = startStop;
	father[startStop] = -1;
	while (head < tail){
		int stopPoped = queue[head++];
		for (indexOfRoute = stopHead[stopPoped]; indexOfRoute != 0; indexOfRoute = routeArray[indexOfRoute].next){
			int route = routeArray[indexOfRoute].route;
			if (routeVisited[route] == 1)
				continue;
			routeVisited[route] = 1;
			for (indexOfStop = routeHead[route]; indexOfStop != 0; indexOfStop = stopArray[indexOfStop].next){
				int stop = stopArray[indexOfStop].stop;
				if (father[stop] != 0)
					continue;
				if (stop == endStop){
					stack[top++] = endStop;
					stack[top++] = stopPoped;
					stop = stopPoped;
					while (father[stop] != -1){
						stack[top] = father[stop];
						top++;
						stop = father[stop];
					}
					int minMoney = top - 1;
					printf("%d\n", minMoney);
					for (top--; top >= 0; top--)
						printf("%d%c", stack[top], top == 0 ? '\n' : ' ');
					return 0;
				}
				
				father[stop] = stopPoped;
				int stopPushed = stop;
				queue[tail++] = stopPushed;
			}//end fo for indexOfStop
		}//end of for indexOfRoute
	}//end of while (head < tail)

	printf("-1");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值