您的位置:首页 > 其它

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

2015-09-12 22:07 423 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息