ural 1434. Buses in Vasyuki
1434. Buses in Vasyuki
Time limit: 3.0 second
Memory limit: 64 MB
Memory limit: 64 MB
The Vasyuki University is holding an ACM contest. In order to help the participants make their stay in the town more comfortable, the organizers composed a scheme of Vasyuki's bus routes and attached it to the invitations together with other useful information.
The Petyuki University is also presented at the contest, but the funding of its team is rather limited. For the sake of economy, the Petyuki students decided to travel between different locations in Vasyuki using the most economical itineraries. They know that buses are the only kind of public transportation in Vasyuki. The price of a ticket is the same for all routes and equals one rouble regardless of the number of stops on the way. If a passenger changes buses, then he or she must buy a new ticket. And the Petyuki students are too lazy to walk. Anyway, it easier for them to write one more program than to walk an extra kilometer. At least, it's quicker.
And what about you? How long will it take you to write a program that determines the most economical itinerary between two bus stops?
P.S. It takes approximately 12 minutes to walk one kilometer.
Input
The first input line contains two numbers: the number of bus routes in Vasyuki N and the total number of bus stops M. The bus stops are assigned numbers from 1 to M. The following N lines contain descriptions of the routes. Each of these lines starts with the number k of stops of the corresponding route, and then k numbers indicating the stops are given ( 1 ≤ N ≤ 1000, 1≤ M ≤ 105, there are in total not more than 200000 numbers in the N lines describing the routes). In theN+2nd line, the numbers A and B of the first and the last stops of the required itinerary are given (numbers A and B are never equal).
Output
If it is impossible to travel from A to B, then output −1. Otherwise, in the first line you should output the minimal amount of money (in roubles) needed for a one-person travel from A to B, and in the second line you should describe one of the most economical routes giving the list of stops where a passenger should change buses (including the stops A and B).
Sample
input | output |
---|---|
3 10 5 2 4 6 8 10 3 3 6 9 2 5 10 5 9 |
3 5 10 6 9 |
Problem Author: Eugine Krokhalev, Ekaterina Vasilyeva
Problem Source: The 7th USU Open Personal Contest - February 25, 2006
Problem Source: The 7th USU Open Personal Contest - February 25, 2006
Tags: graph theory
Difficulty: 728
题意:给出n条公交线,每条公交线有若干个站点
一共有m个站点
每经过一个站点计费1元
最后给出出发点、目的地
问最少多少元及方案。
分析:Spfa。。。。
根据最短路性质。。。。其实就是个bfs
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define For(i, s, t) for(int i = (s); i <= (t); i++) 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 22 #define Rep(i, t) for(int i = (0); i < (t); i++) 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 24 #define rep(i, x, t) for(int i = (x); i < (t); i++) 25 #define MIT (2147483647) 26 #define INF (1000000001) 27 #define MLL (1000000000000000001LL) 28 #define sz(x) ((int) (x).size()) 29 #define clr(x, y) memset(x, y, sizeof(x)) 30 #define puf push_front 31 #define pub push_back 32 #define pof pop_front 33 #define pob pop_back 34 #define ft first 35 #define sd second 36 #define mk make_pair 37 38 39 inline int Getint() 40 { 41 int Ret = 0; 42 char Ch = ' '; 43 bool Flag = 0; 44 while (!(Ch >= '0' && Ch <= '9')) 45 { 46 if (Ch == '-') Flag ^= 1; 47 Ch = getchar(); 48 } 49 while (Ch >= '0' && Ch <= '9') 50 { 51 Ret = Ret * 10 + Ch - '0'; 52 Ch = getchar(); 53 } 54 return Flag ? -Ret : Ret; 55 } 56 57 const int N = 100010, M = 1010; 58 int n, m, St, Ed; 59 vector<int> Bus[M]; 60 vector<int> Index[N]; 61 int Dp[N], From[N], Que[N], Head, Tail; 62 63 inline void Input() 64 { 65 scanf("%d%d", &m, &n); 66 For(i, 1, m) 67 { 68 int s, x; 69 scanf("%d", &s); 70 while(s--) 71 { 72 scanf("%d", &x); 73 Bus[i].pub(x); 74 Index[x].pub(i); 75 } 76 } 77 scanf("%d%d", &St, &Ed); 78 } 79 80 81 inline void Solve() 82 { 83 For(i, 1, n) Dp[i] = INF; 84 Dp[St] = 0, From[St] = 0; 85 Que[Head = Tail = 1] = St; 86 while(Head <= Tail) 87 { 88 int u = Que[Head++]; 89 //printf("%d\n", u); 90 int p = sz(Index[u]); 91 Rep(i, p) 92 { 93 int S = sz(Bus[Index[u][i]]); 94 Rep(j, S) 95 { 96 int v = Bus[Index[u][i]][j]; 97 if(Dp[v] > Dp[u] + 1) 98 { 99 Dp[v] = Dp[u] + 1, From[v] = u; 100 Que[++Tail] = v; 101 } 102 } 103 } 104 } 105 106 if(Dp[Ed] >= INF) puts("-1"); 107 else 108 { 109 printf("%d\n", Dp[Ed]); 110 vector<int> Ans; 111 for(int x = Ed; x; x = From[x]) 112 Ans.pub(x); 113 Ford(i, Dp[Ed], 1) printf("%d ", Ans[i]); 114 printf("%d\n", Ed); 115 } 116 } 117 118 int main() 119 { 120 Input(); 121 Solve(); 122 return 0; 123 }