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. nuget包循环引用问题

    1.项目中有类库YesWay.Nlog.RabbitMQ,依赖项如下YesWay.Nlog.RabbitMQ=>YesWay.Service.Discovery=>YesWay.Log 2 ...

  2. WAF Bypass 笔记(SQL注入篇)

    0x01 背景 waf Bypass 笔记 0x02 服务器特性 1.%特性(ASP+IIS) 在asp+iis的环境中存在一个特性,就是特殊符号%,在该环境下当们我输入s%elect的时候,在WAF ...

  3. ConcurrentModificationException 详解

    工作中碰到个ConcurrentModificationException.代码如下: List list = ...;for(Iterator iter = list.iterator(); ite ...

  4. django rest framwork教程之 viewsets和routers

    ViewSets 和Routers REST框架包括一个用于抽象处理的ViewSets,允许开发人员集中精力对API的状态和交互进行建模,并根据常见约定自动处理URL构造. Viewset 类和 Vi ...

  5. 【linux】 scrapy : Could not find a version that satisfies the requirement Twisted>=13.1.0 (from Scrapy) (from versions: )

    centos7 + python3 安装 scrapy 时候报错,错误信息如下: Could not find a version that satisfies the requirement Twi ...

  6. echarts - 特殊需求实现代码汇总之【饼图】篇

    2018-07-24 15:36:43 起 - 饼图单项不同颜色的设置 效果图: 实现: 说明: 其实很简单,就是设置全局的color属性即可.color属性可以是一套数组,里边的样式以字符串格式设置 ...

  7. 百度前端学院js课堂作业合集+分析(更新中...)

    第一课:简陋的登录框 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  8. SSL是什么?如何使用?

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TLS与 ...

  9. php 函数合并 array_merge 与 + 的区别

    array_merge()是PHP语言中的一个函数,作用是将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面.返回作为结果的数组. 如果输入的数组中有相同的字符串键名,该键的键值为最 ...

  10. (转载)解决AndroidStudio导入项目在 Building gradle project info 一直卡住

    源地址http://blog.csdn.net/yyh352091626/article/details/51490976 Android Studio导入项目的时候,一直卡在Building gra ...