Description

Mike moved to a new city.

There are bus stations in the city, each has a unique name. Each bus has its designated schedule, and sequentially docks at a series of bus stations. Bus lines are bi-directional, and thus you can get on the bus at a station, and get off at any other station in that bus' line. The city provides two kinds of bus services:

1. Type A: each ride costs $2.

2. Type B: rides are completely free of charge.

Given all bus lines in the city, a source station and a destination station, you should help Mike to find the cheapest ride plan to reach the destination from the source.

Input

First line: a positive integer T (T <= 10) indicating the number of test cases.

There are T cases following. In each case, the rst line contains n (1 <= n <= 1,000) indicating the number of bus lines. Then followed by n lines, each of which describes a bus line in the format of t k s1 s2 ... sk (1 <= k <= 10). Speci cally, t is the type of the bus (either A or B), k denotes the number of bus stations in that line, while strings s1,s2,... sk list names of these stations (a bus line may contain duplicated stations) The last line of the case contains two strings: Mike's source s and destination t. All bus station names are case-sensitive alphabets and is no longer than 20. Input guarantees the destination to be reachable.

Output

For each test case: output "Case #x: ans" (without quotes), where x is the number of the case, and ans is the minimum amount of money to reach the destination. 

Sample Input

1
3
A 5 NJU ZSL XJK YT ATZX
B 3 XJK HSDWY MGQ
A 3 HSDWY NJZ MGQ
NJU NJZ

Sample Output

Case #1: 4

spfa的应用,邻接矩阵开不出来,使用邻接表。

 #include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define MAX 10005
#define INF 0x3f3f3f3f
struct Edge{
int from,to,weight;
Edge(int u,int v,int w):from(u),to(v),weight(w){};
};
vector<Edge> E;
vector<int> G[MAX];
void add_edge(int u,int v,int w)
{
E.push_back(Edge(u,v,w));
G[u].push_back(E.size()-);
}
map<string,int> No;
void init()
{
E.clear();
No.clear();
for (int i = ; i<MAX; i++) G[i].clear();
}
bool vis[MAX];
int d[MAX];
void spfa(int st)
{
memset(d,INF,sizeof(d));
memset(vis,false,sizeof(vis));
queue<int> q;
q.push(st);
d[st]=;
vis[st]=;
while (!q.empty())
{
int u=q.front();
q.pop();
vis[u]=;
for(int i=;i<G[u].size();i++)
{
Edge &e=E[G[u][i]];
int tmp=d[e.to];
if(d[e.to]>d[e.from]+e.weight) d[e.to]=d[e.from]+e.weight;
if(d[e.to]<tmp && !vis[e.to])
{
q.push(e.to);
vis[e.to]=;
}
}
}
} int main()
{
int n,t;
scanf("%d",&t);
for(int kase=;kase<=t;kase++)
{
init();
scanf("%d",&n);
int cnt=;
for(int i=;i<=n;i++)
{
char type;
int stop_num;
string stop[];
cin>>type>>stop_num;
for(int j=;j<=stop_num;j++)
{
cin>>stop[j];
if(No.count(stop[j])==) No[stop[j]] = cnt++;
}
for(int u=;u<=stop_num;u++)
{
for(int v=u+;v<=stop_num;v++)
{
int weight=(type == 'A')?:;
add_edge(No[stop[u]], No[stop[v]], weight);
add_edge(No[stop[v]], No[stop[u]], weight);
}
}
}
string st,ed;
cin>>st>>ed;
spfa(No[st]);
printf("Case #%d: %d\n",kase,d[No[ed]]);
}
}

2016江苏省CPC省赛 I - Itinerary Planning的更多相关文章

  1. 2016 CCPC长春重现赛

    1.2016中国大学生程序设计竞赛(长春)-重现赛 2.总结:会做的太少,应变能力也不行,或者说猜题目的能力不行 02  水 04  HDU 5914  Triangle 1.题意:1~n,n个数,问 ...

  2. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  3. 2016 ICPC China-Final 现场赛总结

    距离比赛结束快有一个礼拜了才抽出时间来写这篇总结.今年比赛打了也有5场了(4场区域赛+1场省赛),也取得了不错的成绩(区域赛两银),总的来说第一年就取得这成绩还是挺高兴的.我们队,我自己都渐渐的趋于成 ...

  4. 2016 年青岛网络赛---Family View(AC自动机)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...

  5. 2016 年沈阳网络赛---QSC and Master(区间DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...

  6. 2016 年青岛网络赛---Tea

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5881 Problem Description Tea is good. Tea is life. Te ...

  7. 2016 年青岛网络赛---Sort(k叉哈夫曼)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5884 Problem Description Recently, Bob has just learn ...

  8. 2016 ICPC北京站现场赛总结(再度流水账)

    其他的都先不说,北大的未名湖真的美! 虽然感觉北大其他地方都有些破旧之感,但是未名湖附近真的值得称赞. 在去北京之前就听说北京温度很低,不过是干冷,果不其然.北京不冷,就是嘴唇太干了,不喝水真的受不了 ...

  9. [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题

    第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...

随机推荐

  1. easyui datagrid 单元格编辑(cell editing)

    demo中有row editing 项目中发现个cell editing,但是有bug,修改好了 主要实现功能:单击数据表格单元格,编辑单元格数据 js代码如下: $.extend($.fn.data ...

  2. Java按钮控件数组实现计算器界面

    编写程序,通过按钮数组来管理界面中的所有按钮控件,从而使用最少的代码实现模拟的计算器界面. 思路如下: 创建一个类,通过extends使其继承窗体类JFrame: 创建一个JFrame对象,使用JFr ...

  3. Redis 入门指令

    -- -- string SET key value GET key GETRANGE key start end GETSET key value GETBIT key offset MGET ke ...

  4. actor mysql 持久化之 specified actor

    持久化到mysql,要求一次操作涉及到的多次读写的事务性.使用的 library 是 postgresql-async, akka 版本是 2.11. 1. 实现 per-user 逻辑,简单来讲,就 ...

  5. iOS App Extensions

    一.扩展概述 扩展(Extension)是iOS 8中引入的一个非常重要的新特性.扩展让app之间的数据交互成为可能.用户可以在app中使用其他应用提供的功能,而无需离开当前的应用. 在iOS 8系统 ...

  6. jQuery Colorbox弹窗插件使用教程小结、属性设置详解以及colorbox关闭

    jQuery Colorbox是一款弹出层,内容播放插件,效果极佳,当然我主要是用来弹出图片啦. jQuery Colorbox不仅有弹性动画效果,淡入淡出效果,幻灯片播放,宽度自定义,还能够ajax ...

  7. linux-指定特殊域去重

    测试数据: 2017-10-24 14:14:11:1123 [ INFO] order_type=add,order_id=9152017-10-24 14:14:11:1123 [ INFO] o ...

  8. mysql的联表删除

    联表删除: 1.从数据表t1 中把那些id值在数据表t2 里有匹配的记录全删除掉 DELETE t1 FROM t1,t2 WHERE t1.id=t2.id    或DELETE  FROM t1 ...

  9. IIS URL Rewrite – Installation and Use

    IIS URL Rewrite – Installation and Use Posted by Nick LeFevre | Leave a reply IIS URL Rewrite Instal ...

  10. ISD9160学习笔记03_ISD9160音频解码代码分析

    录音例程涉及了录音和播放两大块内容,这篇笔记就先来说说播放,暂且先击破解码这部分功能. 我的锤子便签中有上个月记下的一句话,“斯蒂芬·平克说,写作之难,在于把网状思考,用树状结构,体现在线性展开的语句 ...