1434. Buses in Vasyuki

Time limit: 3.0 second
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
Difficulty: 728
题意:给出n条公交线,每条公交线有若干个站点
一共有m个站点
每经过一个站点计费1元
最后给出出发点、目的地
问最少多少元及方案。
分析:Spfa。。。。
根据最短路性质。。。。其实就是个bfs
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while (!(Ch >= '' && Ch <= ''))
{
if (Ch == '-') Flag ^= ;
Ch = getchar();
}
while (Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = , M = ;
int n, m, St, Ed;
vector<int> Bus[M];
vector<int> Index[N];
int Dp[N], From[N], Que[N], Head, Tail; inline void Input()
{
scanf("%d%d", &m, &n);
For(i, , m)
{
int s, x;
scanf("%d", &s);
while(s--)
{
scanf("%d", &x);
Bus[i].pub(x);
Index[x].pub(i);
}
}
scanf("%d%d", &St, &Ed);
} inline void Solve()
{
For(i, , n) Dp[i] = INF;
Dp[St] = , From[St] = ;
Que[Head = Tail = ] = St;
while(Head <= Tail)
{
int u = Que[Head++];
//printf("%d\n", u);
int p = sz(Index[u]);
Rep(i, p)
{
int S = sz(Bus[Index[u][i]]);
Rep(j, S)
{
int v = Bus[Index[u][i]][j];
if(Dp[v] > Dp[u] + )
{
Dp[v] = Dp[u] + , From[v] = u;
Que[++Tail] = v;
}
}
}
} if(Dp[Ed] >= INF) puts("-1");
else
{
printf("%d\n", Dp[Ed]);
vector<int> Ans;
for(int x = Ed; x; x = From[x])
Ans.pub(x);
Ford(i, Dp[Ed], ) printf("%d ", Ans[i]);
printf("%d\n", Ed);
}
} int main()
{
Input();
Solve();
return ;
}

ural 1434. Buses in Vasyuki的更多相关文章

  1. URAL 1137Bus Routes (dfs)

    Z - Bus Routes Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  2. 51nod 1434 理解lcm

    1434 区间LCM 题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 一个整数序列S的LCM(最小公倍数)是指最小的正 ...

  3. CF459C Pashmak and Buses (构造d位k进制数

    C - Pashmak and Buses Codeforces Round #261 (Div. 2) C. Pashmak and Buses time limit per test 1 seco ...

  4. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  5. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  6. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  7. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  8. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  9. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

随机推荐

  1. hibernate中的session缓存

    1.什么是session缓存? 在 Session 接口的实现中包含一系列的 Java 集合, 这些 Java 集合构成了 Session 缓存. 只要 Session 实例没有结束生命周期, 且没有 ...

  2. UITapGestureRecognizer

    UITapGestureRecognizer IOS的手势非常多, 但是特别容易其他视图起冲突的手势,要数UITapGestureRecognizer 于是有了gestureRecognizerSho ...

  3. vundle安装 给vim插上翅膀

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) vundle安装方法如下 首先执行以下命令 $ git clone https://githu ...

  4. vimcommandfilepatchcmdfold VIM技巧之分隔窗口 一级精华

    VIM技巧之分隔窗口 分类: 技术2010-07-08 09:57 754人阅读 评论(1) 收藏 举报   同时显示两个不同的文件, 或者同时查看同一个文件的两个不同位置, 或者是同步显示两个文件的 ...

  5. 使用JDBC获取各数据库的Meta信息——表以及对应的列

    先贴代码,作为草稿: 第一个是工具类, MapUtil.java [java] view plain copy import java.util.ArrayList; import java.util ...

  6. HDOJ 1864 最大报销额(01背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1864 最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memor ...

  7. 开发Web Service的几种方式

    本文作者在学习使用Java开发Web Service(不包括Restful)时,由于不知道Java有这么多框架支持开发Web Service一度陷入迷惘,不知道这些框架各有 什么不同,各有什么优缺点. ...

  8. JavaScript toFixed()使用的注意事项

    以下是w3school的定义: 定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 语法 NumberObject.toFixed(num) 参数 描述 num 必 ...

  9. BestCoder18 1002.Math Problem(hdu 5105) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得 ...

  10. codeforces 485B Valuable Resources 解题报告

    题目链接:http://codeforces.com/problemset/problem/485/B 题目意思:需要建造一个正方形的city,这座city的边需要满足平行于 x 轴和 y 轴,而且这 ...